Bagaimana Anda bisa menemukan karakter pertama yang tidak berulang dalam sebuah kata javascript?
Program ini juga sangat bagus untuk pemula yang menguasai framework Java Collection. Ini memberi Anda kesempatan untuk menjelajahi implementasi Peta yang berbeda dan memahami perbedaan antara HashMap dan LinkedHashMap untuk memutuskan kapan menggunakannya. Show
Kami memiliki array Literal Angka/String di mana sebagian besar entri diulang. Tugas kita adalah menulis sebuah fungsi yang menggunakan array ini dan mengembalikan indeks elemen pertama yang tidak muncul secara berurutan Jika tidak ada elemen seperti itu di dalam array, fungsi kita harus mengembalikan -1. Jadi sekarang, mari kita menulis kode untuk fungsi ini. Kami akan menggunakan loop sederhana untuk mengulangi array dan kembali ke tempat kami menemukan karakter yang tidak berulang, jika kami tidak menemukan karakter seperti itu, kami mengembalikan -1 − Contohconst arr = ['d', 'd', 'e', 'e', 'e', 'k', 'j', 'j', 'h']; const firstNonRepeating = arr => { let count = 0; for(let ind = 0; ind < arr.length-1; ind++){ if(arr[ind] !== arr[ind+1]){ if(!count){ return ind; }; count = 0; } else { count++; } }; return -1; }; console.log(firstNonRepeating(arr)); KeluaranOutput di konsol akan menjadi - 5_ Kami akan diberi string input dan tugas kami adalah mengembalikan indeks dari nilai unik pertama (nilai yang tidak berulang). Jika kami tidak menemukan nilai seperti itu, kami akan mengembalikan -1 Pendekatan saya Saya akan mengulangi string input dan melihat semua nilai sebelum dan sesudah nilai saya saat ini, memeriksa duplikat. Jika saya tidak menemukannya, saya hanya akan mengembalikan indeks saat ini Sebaliknya, jika saya berhasil melewati seluruh array dan tidak pernah memenuhi persyaratan tersebut - saya akan mengembalikan -1 KodeDan itu dia "Sebelum" dan "setelah" saya pada baris 3 dan 4 hanyalah snapshot yang membentuk sisa array Dengan menggunakan ini saya dapat menentukan apakah nilai saya saat ini tidak berulang atau tidak Selesai dan selesai… Semoga sampai jumpa di soal tingkat kesulitan menengah AlgoExpert Lebih banyak konten di PlainEnglish. io. Mendaftar untuk buletin mingguan gratis kami. Ikuti kami di Twitter, LinkedIn, YouTube, dan Perselisihan Diberi string S yang terdiri dari Huruf Latin huruf kecil, tugasnya adalah menemukan karakter pertama yang tidak berulang di S Contoh.
Latihan yang Direkomendasikan Karakter yang Tidak BerulangCobalah Pendekatan Naif
Catatan. Dalam python untuk menemukan kemunculan karakter dalam string terdapat string In-Built Function. menghitung() Di bawah ini adalah implementasi dari pendekatan di atas C++
First non-repeating character is f0 First non-repeating character is f1 First non-repeating character is f2 First non-repeating character is f3 First non-repeating character is f4 First non-repeating character is f5 First non-repeating character is f6 First non-repeating character is f3 First non-repeating character is f0 First non-repeating character is f9 First non-repeating character is f3 First non-repeating character is f1 First non-repeating character is f2 First non-repeating character is f3 First non-repeating character is f6 First non-repeating character is f3 First non-repeating character is f6 First non-repeating character is f7 First non-repeating character is f8 First non-repeating character is f9 First non-repeating character is f0 First non-repeating character is f1 First non-repeating character is f2 First non-repeating character is f3 First non-repeating character is f4 First non-repeating character is f3 First non-repeating character is f6 First non-repeating character is f6 First non-repeating character is f0 First non-repeating character is f9 First non-repeating character is f0 First non-repeating character is f1 First non-repeating character is f2 First non-repeating character is f3 First non-repeating character is f4 First non-repeating character is f0 First non-repeating character is f9 First non-repeating character is f3 First non-repeating character is f9 First non-repeating character is f3 First non-repeating character is f1 First non-repeating character is f1 First non-repeating character is f0 First non-repeating character is f3 First non-repeating character is f4 First non-repeating character is f5 First non-repeating character is f6 First non-repeating character is f7 First non-repeating character is f8 First non-repeating character is f3 First non-repeating character is f9 First non-repeating character is f3 First non-repeating character is f1 First non-repeating character is f2 First non-repeating character is f0 First non-repeating character is f3 #include 6 #include 7First non-repeating character is f7 First non-repeating character is f8 First non-repeating character is f3 First non-repeating character is f9 First non-repeating character is f3 using 3 using 4First non-repeating character is f_9
Jawa
________426______8
First non-repeating character is f3 namespace 0 namespace 5 namespace 6 namespace 7First non-repeating character is f0 namespace 9First non-repeating character is f5 First non-repeating character is f6 First non-repeating character is f0 First non-repeating character is f0 std; 4std; 5First non-repeating character is f6 First non-repeating character is f0 First non-repeating character is f1 First non-repeating character is f2 First non-repeating character is f3 First non-repeating character is f6 First non-repeating character is f0 First non-repeating character is f6 First non-repeating character is f7 First non-repeating character is f1 First non-repeating character is f06 First non-repeating character is f3 First non-repeating character is f1 First non-repeating character is f09 First non-repeating character is f5 First non-repeating character is f4 First non-repeating character is f5 First non-repeating character is f6 First non-repeating character is f6 First non-repeating character is f3 First non-repeating character is f9 First non-repeating character is f3 First non-repeating character is f1 First non-repeating character is f2 First non-repeating character is f5 First non-repeating character is f21 std; 5First non-repeating character is f6 First non-repeating character is f3 First non-repeating character is f9 First non-repeating character is f0 First non-repeating character is f9 First non-repeating character is f0 First non-repeating character is f1 First non-repeating character is f30 std; 5First non-repeating character is f32 First non-repeating character is f3 First non-repeating character is f34 First non-repeating character is f4 First non-repeating character is f36 First non-repeating character is f37 First non-repeating character is f6 First non-repeating character is f39 First non-repeating character is f0 First non-repeating character is f9 First non-repeating character is f0 First non-repeating character is f1 First non-repeating character is f2 First non-repeating character is f3 First non-repeating character is f34 #include 6 First non-repeating character is f48 First non-repeating character is f0 First non-repeating character is f9 First non-repeating character is f3 First non-repeating character is f9 First non-repeating character is f_9 Python3First non-repeating character is f_54
First non-repeating character is f55 First non-repeating character is f56 First non-repeating character is f5 First non-repeating character is f58 First non-repeating character is f56 First non-repeating character is f60 std; 5First non-repeating character is f62 First non-repeating character is f56 First non-repeating character is f64 First non-repeating character is f6 First non-repeating character is f66 First non-repeating character is f67 First non-repeating character is f68 First non-repeating character is f3 First non-repeating character is f1 First non-repeating character is f71 First non-repeating character is f56 First non-repeating character is f56 std; 5First non-repeating character is f75 First non-repeating character is f0 First non-repeating character is f62 First non-repeating character is f37 First non-repeating character is f56 First non-repeating character is f66 First non-repeating character is f0 First non-repeating character is f6 First non-repeating character is f3 First non-repeating character is f1 First non-repeating character is f75 First non-repeating character is f0 First non-repeating character is f58 First non-repeating character is f37 First non-repeating character is f56 std; 5First non-repeating character is f1 First non-repeating character is f58 First non-repeating character is f56 First non-repeating character is f56 std; 5First non-repeating character is f75 First non-repeating character is f3 First non-repeating character is f98 First non-repeating character is f7 First non-repeating character is f00 First non-repeating character is f01 First non-repeating character is f1 First non-repeating character is f75 First non-repeating character is f3 First non-repeating character is f98 First non-repeating character is f7 First non-repeating character is f07 First non-repeating character is f08 JavascriptFirst non-repeating character is f09 First non-repeating character is f5 First non-repeating character is f6 First non-repeating character is f_12 First non-repeating character is f13 First non-repeating character is f3 First non-repeating character is f6 First non-repeating character is f6 First non-repeating character is f17 First non-repeating character is f3 First non-repeating character is f1 First non-repeating character is f20 First non-repeating character is f0 First non-repeating character is f4 First non-repeating character is f0 First non-repeating character is f6 First non-repeating character is f6 First non-repeating character is f3 First non-repeating character is f9 First non-repeating character is f1 First non-repeating character is f2 First non-repeating character is f0 First non-repeating character is f4 First non-repeating character is f3 First non-repeating character is f9 First non-repeating character is f_9 First non-repeating character is f1 First non-repeating character is f36 First non-repeating character is f3 First non-repeating character is f38 First non-repeating character is f00 First non-repeating character is f39 First non-repeating character is f9 First non-repeating character is f1 First non-repeating character is f2 First non-repeating character is f3 First non-repeating character is f45 First non-repeating character is f_9
Keluaran First non-repeating character is f_ Kompleksitas Waktu. O(N2), Lintasi string untuk setiap karakter dalam string berukuran N Karakter pertama yang tidak berulang menggunakan fungsi string find()
Pencarian dilakukan menggunakan fungsi find() bawaan Berikut adalah implementasi dari pendekatan tersebut C++First non-repeating character is f_48
First non-repeating character is f54 First non-repeating character is f2
First non-repeating character is f3 First non-repeating character is f6 First non-repeating character is f7 First non-repeating character is f0 First non-repeating character is f60
First non-repeating character is f0 First non-repeating character is f1 First non-repeating character is f63 First non-repeating character is f3 First non-repeating character is f65 First non-repeating character is f3 First non-repeating character is f3 #include 6First non-repeating character is f69 First non-repeating character is f70
First non-repeating character is f3 using 3First non-repeating character is f6 First non-repeating character is f0 First non-repeating character is f9 First non-repeating character is f3 First non-repeating character is f9 First non-repeating character is f3 First non-repeating character is f3 First non-repeating character is f4 First non-repeating character is f3 First non-repeating character is f6 First non-repeating character is f6 First non-repeating character is f3 using 3First non-repeating character is f6 First non-repeating character is f_9
First non-repeating character is f_88 First non-repeating character is f0 First non-repeating character is f1 First non-repeating character is f2
First non-repeating character is f3 First non-repeating character is f93 First non-repeating character is f5 First non-repeating character is f6 First non-repeating character is f3 First non-repeating character is f97 First non-repeating character is f_9
First non-repeating character is f_99 JawaFirst non-repeating character is f_00
First non-repeating character is f02
First non-repeating character is f05 First non-repeating character is f06 First non-repeating character is f05 First non-repeating character is f08 First non-repeating character is f05 namespace 0 namespace 5 namespace 6 First non-repeating character is f13 First non-repeating character is f05 First non-repeating character is f2
First non-repeating character is f3 First non-repeating character is f6 First non-repeating character is f7 First non-repeating character is f0 First non-repeating character is f20 First non-repeating character is f21 First non-repeating character is f22
First non-repeating character is f23 First non-repeating character is f1 First non-repeating character is f25 std; 5First non-repeating character is f27 std; 5First non-repeating character is f32 First non-repeating character is f0 First non-repeating character is f34 #include 6First non-repeating character is f33 First non-repeating character is f0 First non-repeating character is f6 First non-repeating character is f6 First non-repeating character is f23 First non-repeating character is f9 First non-repeating character is f3 First non-repeating character is f9 First non-repeating character is f3 using 3First non-repeating character is f6 First non-repeating character is f05 First non-repeating character is f9 First non-repeating character is f05 namespace 0 namespace 5 namespace 6 First non-repeating character is f50 First non-repeating character is f3 First non-repeating character is f52 First non-repeating character is f5 First non-repeating character is f6 First non-repeating character is f3 First non-repeating character is f97 First non-repeating character is f05 First non-repeating character is f9 First non-repeating character is f_9
First non-repeating character is f60 Python3First non-repeating character is f_61
First non-repeating character is f62 First non-repeating character is f63
First non-repeating character is f3 First non-repeating character is f6 First non-repeating character is f66 First non-repeating character is f67 First non-repeating character is f68
First non-repeating character is f0 First non-repeating character is f1 First non-repeating character is f71 First non-repeating character is f37 std; 5First non-repeating character is f74 First non-repeating character is f56 First non-repeating character is f56 First non-repeating character is f60 std; 5First non-repeating character is f_75
First non-repeating character is f3 First non-repeating character is f98 First non-repeating character is f7 First non-repeating character is f07 First non-repeating character is f84
First non-repeating character is f3 First non-repeating character is f6
First non-repeating character is f3 using 3
First non-repeating character is f_89
First non-repeating character is f90 First non-repeating character is f56 First non-repeating character is f92
First non-repeating character is f_93 C#First non-repeating character is f_94 First non-repeating character is f_95
First non-repeating character is f97
First non-repeating character is f3 First non-repeating character is f06 First non-repeating character is f3 First non-repeating character is f08 First non-repeating character is f3 namespace 0 namespace 5 namespace 6 First non-repeating character is f10 First non-repeating character is f55 First non-repeating character is f12 First non-repeating character is f3 First non-repeating character is f2
First non-repeating character is f0 First non-repeating character is f6 First non-repeating character is f7 First non-repeating character is f0 First non-repeating character is f19
First non-repeating character is f3 First non-repeating character is f1 First non-repeating character is f22 First non-repeating character is f5 First non-repeating character is f24 First non-repeating character is f5________2______26 First non-repeating character is f27 #include 6First non-repeating character is f27 First non-repeating character is f30
First non-repeating character is f5 First non-repeating character is f6 First non-repeating character is f6 First non-repeating character is f3 First non-repeating character is f9 First non-repeating character is f0 First non-repeating character is f9 First non-repeating character is f0 using 3First non-repeating character is f6 First non-repeating character is f3 First non-repeating character is f9
First non-repeating character is f3 First non-repeating character is f88 First non-repeating character is f3 First non-repeating character is f46 namespace 5 namespace 6 First non-repeating character is f49 First non-repeating character is f3 First non-repeating character is f2
First non-repeating character is f0 First non-repeating character is f55 First non-repeating character is f54 First non-repeating character is f5 First non-repeating character is f6 First non-repeating character is f0 First non-repeating character is f97 First non-repeating character is f3 First non-repeating character is f9 First non-repeating character is f_9
First non-repeating character is f_62 JavascriptFirst non-repeating character is f_63
First non-repeating character is f_64
First non-repeating character is f65 First non-repeating character is f66
First non-repeating character is f67 First non-repeating character is f6 First non-repeating character is f69 First non-repeating character is f67 First non-repeating character is f2
First non-repeating character is f72 First non-repeating character is f1 First non-repeating character is f74 First non-repeating character is f72 First non-repeating character is f2 First non-repeating character is f77 First non-repeating character is f78
First non-repeating character is f77 First non-repeating character is f6 First non-repeating character is f72 First non-repeating character is f9 First non-repeating character is f67 First non-repeating character is f9 First non-repeating character is f67 using 3First non-repeating character is f_9
First non-repeating character is f_88 First non-repeating character is f89 First non-repeating character is f92 First non-repeating character is f_93
First non-repeating character is f_99
First non-repeating character is f_93 Keluaran First non-repeating character is f_ Kompleksitas Waktu. O(N2) Karakter non-berulang pertama menggunakan HashMap dan dua traversal string
|