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 blackDiberi 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 sMemasukkan. 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