Làm cách nào để tìm các ký tự lặp lại trong một chuỗi trong JavaScript?

Chúng tôi được yêu cầu viết một hàm JavaScript nhận vào một chuỗi và trả về một chuỗi mới chỉ chứa các từ xuất hiện nhiều lần trong chuỗi ban đầu

Ví dụ. Nếu chuỗi đầu vào là -

const str = "big black bug bit a big black dog on his big black nose";

Sau đó, đầu ra phải là -

const output = "big black";

Thí dụ

Hãy viết mã cho chức năng này -

const str = "big black bug bit a big black dog on his big black nose";
const findDuplicateWords = str => {
   const strArr = str.split[" "];
   const res = [];
   for[let i = 0; i < strArr.length; i++]{
      if[strArr.indexOf[strArr[i]] !== strArr.lastIndexOf[strArr[i]]]{
         if[!res.includes[strArr[i]]]{
            res.push[strArr[i]];
         };
      };
   };
   return res.join[" "];
};
console.log[findDuplicateWords[str]];

đầu ra

Đầu ra trong bảng điều khiển. -

big black

Trong bài viết này, chúng ta được cung cấp một chuỗi, nhiệm vụ của chúng ta là tìm sự xuất hiện của một ký tự trong chuỗi với sự trợ giúp của một hàm do người dùng định nghĩa.  

Example:
Input : "hello"
Output : h occur 1 times
         e occur 1 times
         l occur 2 times
         o occur 1 times
Explanation : here "hello" have 1 h, so it have 1 value.
               as same e have 1, l have 2 , o have 1.
Example 2:
Input : "did"
Output: d occur 2 times 
        i occur 1 times

Cách tiếp cận 1. Theo cách tiếp cận này, chúng tôi sử dụng cấu trúc dữ liệu bản đồ để lưu trữ số lần các ký tự xuất hiện

  • Đầu tiên, chúng tôi khởi tạo bản đồ bằng một khóa cho mỗi ký tự của chuỗi và giá trị cho mỗi ký tự là 0
  • Chúng tôi lặp qua chuỗi và tăng giá trị của ký tự
  • Cuối cùng, in khóa-giá trị của bản đồ

Thí dụ. Ví dụ này cho thấy việc sử dụng phương pháp được giải thích ở trên

Javascript




    //function to print occurrence of character

    function printans[ ans ]

    

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
1

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______5_______3
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
4

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______5_______1

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
8

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7
g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
0

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
1

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2
g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

    

    

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

    

    

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
9

    function

h occur 1 times
e occur 1 times
l occur 2 times
o occur 1 times
2

    

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
1

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______5_______3
h occur 1 times
e occur 1 times
l occur 2 times
o occur 1 times
7

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______5_______1

    

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
72

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
74

5

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2
g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2    0

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2    2

    

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

    

        7

    function //function to print occurrence of character0

    

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
1

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______133_______4

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______133_______6//function to print occurrence of character7

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______5_______1

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7    1    2    3

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7    5     6

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2
g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2function0

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______5_______1

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7function4

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7function6function7 function8

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
3printans[ ans ]1

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
1

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
1printans[ ans ]5

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7
g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7_______5_______00

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2
g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

    

    

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

    

    

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
09

    5_______11_______5_______12    6

    

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
15

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
16

đầu ra

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times

Cách tiếp cận 2. Trong cách tiếp cận này, chúng tôi sử dụng vòng lặp for lồng nhau để lặp qua chuỗi và đếm cho từng ký tự trong chuỗi.  

  • Đầu tiên, khởi tạo count với giá trị 0 cho giá trị thứ i của chuỗi
  • Bây giờ chúng tôi lặp lại chuỗi nếu giá trị thứ i khớp với ký tự, tăng giá trị đếm lên 1
  • Cuối cùng, in giá trị của số đếm

Thí dụ. Ví dụ này cho thấy việc sử dụng phương pháp được giải thích ở trên

Javascript




    

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
19

    function

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
22

    

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
1

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______5_______26

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______133_______6
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
29

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______5_______1

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7    1
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
34
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
35

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7    5    6

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2
g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______5_______42

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______5_______3
h occur 1 times
e occur 1 times
l occur 2 times
o occur 1 times
7

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2_______5_______1

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
49

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
51

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
3
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
54

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
1

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
1//function to print occurrence of character6
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
59

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
1
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
1

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
62_______5_______63    6

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
1
g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
1//function to print occurrence of character6
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
69

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
1
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
1

5

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
73

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
1
g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7
g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7//function to print occurrence of character6
h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
80

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7_______5_______82

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
7

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
2
g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

    

    

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times
3

    

    

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
09

    5_______93_______5_______94    6

    

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
97

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times
16

đầu ra

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times

Cách tiếp cận-3. Trong cách tiếp cận này, chúng tôi sẽ sử dụng vòng lặp for để lặp qua chính chuỗi hoàn chỉnh. Đây là cách tiếp cận đơn giản nhất mà chúng tôi có thể thực hiện để tìm ra nhiệm vụ kết quả

Làm cách nào để tìm các từ lặp lại trong một chuỗi trong JavaScript?

Để làm điều đó, hãy sử dụng chuỗi. .
tách dấu chấm câu khỏi chuỗi với. thay thế[]
thay đổi tất cả ký tự thành chữ thường với. toLowerCase[]
chuyển đổi chuỗi thành mảng từ với. tách ra[]
lặp qua từng từ [. forEach ], thêm nó vào một đối tượng đếm từ

Làm cách nào để tìm các ký tự lặp lại trong một mảng trong JavaScript?

Tìm các giá trị trùng lặp trong một mảng bằng javaScript .
Sử dụng phương thức indexOf[]
Sử dụng phương thức has[]
Sử dụng một cặp đối tượng & giá trị khóa
Sử dụng chức năng "một số"
Sử dụng phép lặp

Làm cách nào để kiểm tra xem chuỗi có chứa cùng một ký tự trong JavaScript không?

Bạn có thể kiểm tra xem chuỗi JavaScript có chứa ký tự hoặc cụm từ hay không bằng cách sử dụng phương thức include[], indexOf[] hoặc biểu thức chính quy . bao gồm [] là phương pháp phổ biến nhất để kiểm tra xem một chuỗi có chứa một chữ cái hoặc một loạt các chữ cái hay không và được thiết kế riêng cho mục đích đó.

Chủ Đề