Temukan karakter duplikat dalam string javascript

Kami diminta untuk menulis fungsi JavaScript yang mengambil string dan mengembalikan string baru hanya dengan kata-kata yang muncul lebih dari sekali dalam string asli

Misalnya. Jika string input adalah -

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

Maka hasilnya harus -

const output = "big black";
_

Contoh

Mari tulis kode untuk fungsi ini −

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));

Keluaran

Keluaran di konsol. −

big black

Temukan karakter duplikat dalam string javascript


Temukan karakter duplikat dalam string javascript

Diberi string str, tugasnya adalah menemukan semua karakter duplikat yang ada dalam string tertentu dalam urutan leksikografis tanpa menggunakan struktur data tambahan apa pun

Contoh

Memasukkan. str = “geeksforgeeks”
Keluaran. misalnya g k s
Penjelasan.  
Frekuensi karakter 'g' = 2
Frekuensi karakter 'e' = 4
Frekuensi karakter 'k' = 2
Frekuensi karakter 's' = 2
Oleh karena itu, output yang dibutuhkan adalah misalnya g k s

Memasukkan. str = “apel”
Keluaran. P
Penjelasan.  
Frekuensi karakter 'p' = 2.  
Oleh karena itu, output yang dibutuhkan adalah p

Direkomendasikan. Harap coba pendekatan Anda pada {IDE} terlebih dahulu, sebelum melanjutkan ke solusi

Mendekati. Ikuti langkah-langkah di bawah ini untuk menyelesaikan masalah

  • Inisialisasi variabel, katakan dulu, di mana bit pertama memeriksa apakah karakter (i + 'a') hadir dalam string setidaknya sekali atau tidak
  • Inisialisasi sebuah variabel, katakanlah yang kedua, di mana bit kedua memeriksa apakah karakter (i + 'a') ada dalam string setidaknya dua kali atau tidak
  • Ulangi karakter string. Untuk setiap karakter ke-i, periksa apakah str[i] sudah muncul di string atau belum. Jika ternyata benar, maka atur (str[i] – ‘a’) bit ke-dua
  • Jika tidak, atur (str[i] – ‘a’) bit ke-1 terlebih dahulu
  • Terakhir, ulangi rentang [0, 25] dan periksa apakah bit pertama dan kedua disetel atau tidak. Jika ternyata benar, maka cetak (i ​​+ 'a')

Di bawah ini adalah implementasi dari pendekatan di atas

C++




// C++ program to implement_

// the above approach

#include <bits/stdc++.h>_

using namespace std;

 

// Function to find duplicate characters

// in string without using any additional

// data structure

// C++ program to implement0 // C++ program to implement1// C++ program to implement2 // C++ program to implement3

// C++ program to implement_4

 

// C++ program to implement_5// C++ program to implement6

// C++ program to implement_5// C++ program to implement8

// C++ program to implement5// C++ program to implement2 // the above approach1

 

// C++ program to implement_5// C++ program to implement6

// C++ program to implement5// the above approach5

// C++ program to implement5// C++ program to implement2 // the above approach8

 

// C++ program to implement5#include <bits/stdc++.h>0

// C++ program to implement5#include <bits/stdc++.h>2

// C++ program to implement5#include <bits/stdc++.h>4 #include <bits/stdc++.h>5// C++ program to implement2 #include <bits/stdc++.h>7

 

#include <bits/stdc++.h>8#include <bits/stdc++.h>9

#include <bits/stdc++.h>8using1 using2using3using4

 

using5using6

using5using8

using9namespace0using3namespace2

#include <bits/stdc++.h>8namespace4

#include <bits/stdc++.h>8namespace6 // C++ program to implement4

 

using5using6

using5std;1

using9std;3using3namespace2

#include <bits/stdc++.h>8namespace4

// C++ program to implement5namespace4

 

// C++ program to implement_5// Function to find duplicate characters1

// C++ program to implement5#include <bits/stdc++.h>4 #include <bits/stdc++.h>5// C++ program to implement2 // Function to find duplicate characters6

 

#include <bits/stdc++.h>8// Function to find duplicate characters8

#include <bits/stdc++.h>8// in string without using any additional0

#include <bits/stdc++.h>8using1 // in string without using any additional3

using5// in string without using any additional5

 

using5// in string without using any additional7// in string without using any additional8// in string without using any additional9using3// data structure1// data structure2// data structure3

#include <bits/stdc++.h>8namespace4

// C++ program to implement5namespace4

namespace_4

 

// data structure_9

// C++ program to implement2 // C++ program to implement01

// C++ program to implement_4

// C++ program to implement5// C++ program to implement04// C++ program to implement05// data structure3

// C++ program to implement_5// C++ program to implement2 // C++ program to implement09

 

// C++ program to implement_5// C++ program to implement11

namespace_4

Jawa




// C++ program to implement_13

// C++ program to implement_14 // C++ program to implement15 // C++ program to implement16

// C++ program to implement_4

 

// C++ program to implement18// Function to find duplicate characters

// C++ program to implement18// in string without using any additional

// C++ program to implement18// data structure

// C++ program to implement18// C++ program to implement25 // C++ program to implement0 // C++ program to implement27// C++ program to implement2 // C++ program to implement3

// C++ program to implement18// C++ program to implement4

 

// C++ program to implement_5// C++ program to implement6

// C++ program to implement_5// C++ program to implement8

// C++ program to implement5// C++ program to implement2 // C++ program to implement38// C++ program to implement39// data structure3

 

// C++ program to implement_5// C++ program to implement6

// C++ program to implement5// the above approach5

// C++ program to implement5// C++ program to implement2 // C++ program to implement47// C++ program to implement39// data structure3

 

// C++ program to implement5#include <bits/stdc++.h>0

// C++ program to implement5#include <bits/stdc++.h>2

// C++ program to implement5#include <bits/stdc++.h>4 #include <bits/stdc++.h>5// C++ program to implement2 // C++ program to implement58// C++ program to implement39// C++ program to implement60

// C++ program to implement_5// C++ program to implement4

 

// C++ program to implement_63#include <bits/stdc++.h>9

// C++ program to implement_63using1 // C++ program to implement67// C++ program to implement68 // C++ program to implement69using3// C++ program to implement71// C++ program to implement39// C++ program to implement73

// C++ program to implement_63// C++ program to implement4

 

#include <bits/stdc++.h>8using6

#include <bits/stdc++.h>8using8

// C++ program to implement80// C++ program to implement81// C++ program to implement68 // C++ program to implement69using3namespace2

// C++ program to implement_63namespace4

// C++ program to implement_63namespace6

// C++ program to implement_63// C++ program to implement4

 

#include <bits/stdc++.h>8using6

#include <bits/stdc++.h>8std;1

// C++ program to implement80// C++ program to implement97// C++ program to implement68 // C++ program to implement69using3namespace2

// C++ program to implement_63namespace4

// C++ program to implement5namespace4

 

// C++ program to implement_5// Function to find duplicate characters1

// C++ program to implement5#include <bits/stdc++.h>4 #include <bits/stdc++.h>5// C++ program to implement2 // C++ program to implement58// C++ program to implement39// the above approach14// the above approach15// the above approach16

// C++ program to implement_5// C++ program to implement4

 

// C++ program to implement_63// Function to find duplicate characters8

// C++ program to implement_63// in string without using any additional0

// C++ program to implement63using1 // the above approach25// C++ program to implement68 ________5______27

// the above approach28// the above approach29// C++ program to implement68 // the above approach31________4______39// the above approach33

 

#include <bits/stdc++.h>8// the above approach35// in string without using any additional8// the above approach37using3// the above approach39// data structure2// the above approach41

// C++ program to implement_63namespace4

// C++ program to implement5namespace4

// C++ program to implement18namespace4

 

// C++ program to implement_18// data structure9

// C++ program to implement18// C++ program to implement25 // C++ program to implement14 // C++ program to implement0 // the above approach54

// C++ program to implement18// C++ program to implement4

// C++ program to implement5// the above approach58// C++ program to implement05// data structure3

// C++ program to implement_5// C++ program to implement2 // C++ program to implement09

 

// C++ program to implement_5// C++ program to implement11

// C++ program to implement18namespace4

namespace_4

 

// the above approach_69

Python3




// the above approach_70

// the above approach_71

 

// the above approach_72

// the above approach_73

// the above approach_74

// the above approach75 // the above approach76

// C++ program to implement_18

// C++ program to implement5// the above approach79

// C++ program to implement5// the above approach81

// C++ program to implement5std;1// the above approach84 // C++ program to implement39

 

// C++ program to implement5// the above approach79

// C++ program to implement5// the above approach89

// C++ program to implement5using8// the above approach84 // C++ program to implement39

 

// C++ program to implement_5// the above approach95

// C++ program to implement5// the above approach97

// C++ program to implement5#include <bits/stdc++.h>4 #include <bits/stdc++.h>00#include <bits/stdc++.h>01 #include <bits/stdc++.h>02#include <bits/stdc++.h>03

// C++ program to implement_63

#include <bits/stdc++.h>8#include <bits/stdc++.h>06

#include <bits/stdc++.h>8using1 #include <bits/stdc++.h>09// C++ program to implement68 #include <bits/stdc++.h>11#include <bits/stdc++.h>12#include <bits/stdc++.h>13#include <bits/stdc++.h>14 #include <bits/stdc++.h>15________6______16

// C++ program to implement_80

using5#include <bits/stdc++.h>19

using5using8// the above approach84 #include <bits/stdc++.h>23// C++ program to implement68 #include <bits/stdc++.h>11#include <bits/stdc++.h>12#include <bits/stdc++.h>13#include <bits/stdc++.h>14 ________6______15________6______30

#include <bits/stdc++.h>8namespace6#include <bits/stdc++.h>33

using5#include <bits/stdc++.h>19

using5std;1// the above approach84 #include <bits/stdc++.h>39// C++ program to implement68 #include <bits/stdc++.h>11#include <bits/stdc++.h>12#include <bits/stdc++.h>13#include <bits/stdc++.h>14 ________6______15________6______30

 

// C++ program to implement5#include <bits/stdc++.h>48

// C++ program to implement5#include <bits/stdc++.h>4 #include <bits/stdc++.h>00#include <bits/stdc++.h>01 #include <bits/stdc++.h>02#include <bits/stdc++.h>5// the above approach15#include <bits/stdc++.h>56

// C++ program to implement_63

#include <bits/stdc++.h>8#include <bits/stdc++.h>59

#include <bits/stdc++.h>8#include <bits/stdc++.h>61

#include <bits/stdc++.h>8using1 // C++ program to implement67// C++ program to implement68 // the above approach27#include <bits/stdc++.h>67 #include <bits/stdc++.h>68// C++ program to implement68 #include <bits/stdc++.h>70

using5#include <bits/stdc++.h>72#include <bits/stdc++.h>5________6______74#include <bits/stdc++.h>75#include <bits/stdc++.h>76 #include <bits/stdc++.h>15#include <bits/stdc++.h>78// the above approach84 ________12______2________4______73

Bagaimana menemukan karakter duplikat dalam string JavaScript?

+ (1 + n - (n-1)) = (n-1)*(1 + n) - (n)(n-1)/2 = (n^2 + n - 2) . Jadi sebaiknya menggunakan objek untuk memetakan dan mengingat karakter untuk memeriksa keunikan atau duplikat . Dengan asumsi ukuran data maksimum untuk setiap karakter, proses ini akan menjadi algoritma O(n).

Bagaimana cara menemukan karakter duplikat dalam sebuah string?

Untuk menemukan karakter duplikat dari string, kami menghitung kemunculan setiap karakter dalam string . Jika count lebih besar dari 1, ini menyiratkan bahwa sebuah karakter memiliki entri duplikat dalam string. Pada contoh di atas, karakter yang disorot dengan warna hijau adalah karakter duplikat.

Bagaimana menemukan kata duplikat dalam string di JavaScript?

Temukan duplikat dalam larik menggunakan javaScript .
Menggunakan metode indexOf()
Menggunakan metode has()
Menggunakan objek & pasangan nilai kunci
Menggunakan fungsi "beberapa".
Menggunakan iterasi

Bagaimana cara memeriksa apakah string berisi karakter yang sama di JavaScript?

Anda dapat memeriksa apakah string JavaScript berisi karakter atau frasa menggunakan metode includes(), indexOf(), atau ekspresi reguler . include() adalah metode yang paling umum untuk memeriksa apakah suatu string berisi huruf atau rangkaian huruf, dan dirancang khusus untuk tujuan itu.