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.  

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 −

Contoh

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

Keluaran

Output di konsol akan menjadi -

5
_

Bagaimana Anda bisa menemukan karakter pertama yang tidak berulang dalam sebuah kata javascript?


Bagaimana Anda bisa menemukan karakter pertama yang tidak berulang dalam sebuah kata javascript?

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

Kode

Dan 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.  

Memasukkan. “geeksforgeeks”
Keluaran. F
Penjelasan. Karena 'f' adalah karakter pertama dalam string yang tidak berulang

Bagaimana Anda bisa menemukan karakter pertama yang tidak berulang dalam sebuah kata javascript?

Memasukkan. “algoritma”
Keluaran. A
Penjelasan. Karena 'a' adalah karakter pertama dalam string yang tidak berulang

Latihan yang Direkomendasikan Karakter yang Tidak BerulangCobalah

Pendekatan Naif

Idenya adalah untuk mengulang string dan untuk setiap karakter memeriksa kemunculan karakter yang sama dalam string. Jika hitungan kemunculannya adalah 1 maka kembalikan karakter itu. Jika tidak, cari karakter yang tersisa

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++




#include

using namespace std;

 

First non-repeating character is f
0
First non-repeating character is f
1

First non-repeating character is f
2

First non-repeating character is f
3
First non-repeating character is f
4
First non-repeating character is f
5
First non-repeating character is f
6

First non-repeating character is f
3
First non-repeating character is f
0
First non-repeating character is f
9

First non-repeating character is f
3
First non-repeating character is f
1
First non-repeating character is f
2
First non-repeating character is f
3
First non-repeating character is f
6

First non-repeating character is f
3
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
9

First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
3
First non-repeating character is f
4

First non-repeating character is f
3
First non-repeating character is f
6
First non-repeating character is f
6

First non-repeating character is f
0
First non-repeating character is f
9

First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
3
First non-repeating character is f
4

First non-repeating character is f
0
First non-repeating character is f
9

First non-repeating character is f
3
First non-repeating character is f
9

First non-repeating character is f
3
First non-repeating character is f
1
First non-repeating character is f
1

First non-repeating character is f
0
First non-repeating character is f
3
First non-repeating character is f
4

First non-repeating character is f
5
First non-repeating character is f
6

First non-repeating character is f
7
First non-repeating character is f
8

First non-repeating character is f
3
First non-repeating character is f
9

First non-repeating character is f
3
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
0
First non-repeating character is f
3#include 6 #include 7

First non-repeating character is f
7
First non-repeating character is f
8

First non-repeating character is f
3
First non-repeating character is f
9

First non-repeating character is f
3using3 using4

First non-repeating character is f
_9

 

using6

Jawa




using_7

 

________426______8 using9

 

namespace0 namespace1 namespace2

First non-repeating character is f
3namespace0 namespace5 namespace6 namespace7

First non-repeating character is f
0namespace9
First non-repeating character is f
5
First non-repeating character is f
6

First non-repeating character is f
0
First non-repeating character is f
0 std;4std;5
First non-repeating character is f
6

First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
2
First non-repeating character is f
3
First non-repeating character is f
6

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
06

First non-repeating character is f
3
First non-repeating character is f
1
First non-repeating character is f
09

First non-repeating character is f
5
First non-repeating character is f
4

First non-repeating character is f
5
First non-repeating character is f
6
First non-repeating character is f
6

First non-repeating character is f
3
First non-repeating character is f
9

First non-repeating character is f
3
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
5
First non-repeating character is f
21std;5
First non-repeating character is f
6

First non-repeating character is f
3
First non-repeating character is f
9

First non-repeating character is f
0
First non-repeating character is f
9

First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
30std;5
First non-repeating character is f
32

First non-repeating character is f
3
First non-repeating character is f
34
First non-repeating character is f
4

First non-repeating character is f
36
First non-repeating character is f
37
First non-repeating character is f
6
First non-repeating character is f
39

First non-repeating character is f
0
First non-repeating character is f
9

First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
3
First non-repeating character is f
34#include 6
First non-repeating character is f
48

First non-repeating character is f
0
First non-repeating character is f
9

First non-repeating character is f
3
First non-repeating character is f
9

First non-repeating character is f
_9

Python3




First non-repeating character is f
_54

 

First non-repeating character is f
55
First non-repeating character is f
56
First non-repeating character is f
5

First non-repeating character is f
58
First non-repeating character is f
56
First non-repeating character is f
60std;5

First non-repeating character is f
62
First non-repeating character is f
56
First non-repeating character is f
64

First non-repeating character is f
6
First non-repeating character is f
66
First non-repeating character is f
67
First non-repeating character is f
68

First non-repeating character is f
3
First non-repeating character is f
1
First non-repeating character is f
71
First non-repeating character is f
56
First non-repeating character is f
56 std;5
First non-repeating character is f
75

First non-repeating character is f
0
First non-repeating character is f
62
First non-repeating character is f
37
First non-repeating character is f
56
First non-repeating character is f
66

First non-repeating character is f
0
First non-repeating character is f
6

First non-repeating character is f
3
First non-repeating character is f
1
First non-repeating character is f
75

First non-repeating character is f
0
First non-repeating character is f
58
First non-repeating character is f
37
First non-repeating character is f
56 std;5

First non-repeating character is f
1
First non-repeating character is f
58
First non-repeating character is f
56
First non-repeating character is f
56 std;5
First non-repeating character is f
75

First non-repeating character is f
3
First non-repeating character is f
98
First non-repeating character is f
7
First non-repeating character is f
00
First non-repeating character is f
01

First non-repeating character is f
1
First non-repeating character is f
75

First non-repeating character is f
3
First non-repeating character is f
98
First non-repeating character is f
7
First non-repeating character is f
07
First non-repeating character is f
08

Javascript




First non-repeating character is f
09
First non-repeating character is f
5
First non-repeating character is f
6

First non-repeating character is f
_12

First non-repeating character is f
13
First non-repeating character is f
3
First non-repeating character is f
6

First non-repeating character is f
6
First non-repeating character is f
17

First non-repeating character is f
3
First non-repeating character is f
1
First non-repeating character is f
20

First non-repeating character is f
0
First non-repeating character is f
4

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
6

First non-repeating character is f
3
First non-repeating character is f
9
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
0
First non-repeating character is f
4

First non-repeating character is f
3
First non-repeating character is f
9

First non-repeating character is f
_9

First non-repeating character is f
1
First non-repeating character is f
36

First non-repeating character is f
3
First non-repeating character is f
38
First non-repeating character is f
00
First non-repeating character is f
39

First non-repeating character is f
9
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
3
First non-repeating character is f
45

First non-repeating character is f
_9

 

using6

Keluaran

First non-repeating character is f
_

Kompleksitas Waktu. O(N2), Lintasi string untuk setiap karakter dalam string berukuran N
Ruang Bantu. O(1)

Karakter pertama yang tidak berulang menggunakan fungsi string find()

Idenya adalah untuk mencari karakter saat ini dalam string tepat setelah kemunculan pertamanya dalam string. Jika karakter ditemukan di string yang tersisa, kembalikan karakter itu.  

Pencarian dilakukan menggunakan fungsi find() bawaan

Berikut adalah implementasi dari pendekatan tersebut

C++




First non-repeating character is f
_48

 

#include

using namespace std;

 

namespace6

First non-repeating character is f
54

First non-repeating character is f
2

 

First non-repeating character is f
3
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
0
First non-repeating character is f
60

 

First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
63

First non-repeating character is f
3
First non-repeating character is f
65

First non-repeating character is f
3
First non-repeating character is f
3#include 6

First non-repeating character is f
69
First non-repeating character is f
70

 

First non-repeating character is f
3using3
First non-repeating character is f
6

First non-repeating character is f
0
First non-repeating character is f
9

First non-repeating character is f
3
First non-repeating character is f
9

First non-repeating character is f
3
First non-repeating character is f
3
First non-repeating character is f
4

First non-repeating character is f
3
First non-repeating character is f
6
First non-repeating character is f
6

First non-repeating character is f
3using3
First non-repeating character is f
6

First non-repeating character is f
_9

 

First non-repeating character is f
_88

First non-repeating character is f
0
First non-repeating character is f
1

First non-repeating character is f
2

 

First non-repeating character is f
3
First non-repeating character is f
93
First non-repeating character is f
5
First non-repeating character is f
6

First non-repeating character is f
3
First non-repeating character is f
97

First non-repeating character is f
_9

 

First non-repeating character is f
_99

Jawa




First non-repeating character is f
_00

using8

First non-repeating character is f
02

 

namespace1 namespace2

 

First non-repeating character is f
05
First non-repeating character is f
06

First non-repeating character is f
05
First non-repeating character is f
08

First non-repeating character is f
05namespace0 namespace5 namespace6
First non-repeating character is f
13

First non-repeating character is f
05
First non-repeating character is f
2

 

First non-repeating character is f
3
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
0
First non-repeating character is f
20
First non-repeating character is f
21
First non-repeating character is f
22

 

First non-repeating character is f
23
First non-repeating character is f
1
First non-repeating character is f
25std;5
First non-repeating character is f
27std;5
First non-repeating character is f
32

First non-repeating character is f
0
First non-repeating character is f
34#include 6
First non-repeating character is f
33

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
6

First non-repeating character is f
23
First non-repeating character is f
9

First non-repeating character is f
3
First non-repeating character is f
9

First non-repeating character is f
3using3
First non-repeating character is f
6

First non-repeating character is f
05
First non-repeating character is f
9

First non-repeating character is f
05namespace0 namespace5 namespace6
First non-repeating character is f
50

First non-repeating character is f
3
First non-repeating character is f
52
First non-repeating character is f
5
First non-repeating character is f
6

First non-repeating character is f
3
First non-repeating character is f
97

First non-repeating character is f
05
First non-repeating character is f
9

First non-repeating character is f
_9

 

First non-repeating character is f
60

Python3




First non-repeating character is f
_61

 

 

First non-repeating character is f
62
First non-repeating character is f
63

 

First non-repeating character is f
3
First non-repeating character is f
6
First non-repeating character is f
66
First non-repeating character is f
67
First non-repeating character is f
68

 

First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
71
First non-repeating character is f
37std;5
First non-repeating character is f
74
First non-repeating character is f
56
First non-repeating character is f
56
First non-repeating character is f
60std;5
First non-repeating character is f
_75

 

First non-repeating character is f
3
First non-repeating character is f
98
First non-repeating character is f
7
First non-repeating character is f
07
First non-repeating character is f
84

 

First non-repeating character is f
3
First non-repeating character is f
6

 

First non-repeating character is f
3using3

 

First non-repeating character is f
_89

 

 

First non-repeating character is f
90
First non-repeating character is f
56
First non-repeating character is f
92

 

First non-repeating character is f
_93

C#




First non-repeating character is f
_94

First non-repeating character is f
_95

using

First non-repeating character is f
97

 

namespace0 namespace5 namespace1 namespace2

 

First non-repeating character is f
3
First non-repeating character is f
06

First non-repeating character is f
3
First non-repeating character is f
08

First non-repeating character is f
3namespace0 namespace5 namespace6
First non-repeating character is f
10
First non-repeating character is f
55
First non-repeating character is f
12

First non-repeating character is f
3
First non-repeating character is f
2

 

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
0
First non-repeating character is f
19

 

First non-repeating character is f
3
First non-repeating character is f
1
First non-repeating character is f
22

First non-repeating character is f
5
First non-repeating character is f
24

First non-repeating character is f
5________2______26

First non-repeating character is f
27#include 6

First non-repeating character is f
27
First non-repeating character is f
30

 

First non-repeating character is f
5
First non-repeating character is f
6
First non-repeating character is f
6

First non-repeating character is f
3
First non-repeating character is f
9

First non-repeating character is f
0
First non-repeating character is f
9

First non-repeating character is f
0using3
First non-repeating character is f
6

First non-repeating character is f
3
First non-repeating character is f
9

 

First non-repeating character is f
3
First non-repeating character is f
88

First non-repeating character is f
3
First non-repeating character is f
46 namespace5 namespace6
First non-repeating character is f
49

First non-repeating character is f
3
First non-repeating character is f
2

 

First non-repeating character is f
0
First non-repeating character is f
55
First non-repeating character is f
54
First non-repeating character is f
5
First non-repeating character is f
6

First non-repeating character is f
0
First non-repeating character is f
97

First non-repeating character is f
3
First non-repeating character is f
9

First non-repeating character is f
_9

 

First non-repeating character is f
_62

Javascript




First non-repeating character is f
_63

 

First non-repeating character is f
_64

 

First non-repeating character is f
65
First non-repeating character is f
66

 

First non-repeating character is f
67
First non-repeating character is f
6
First non-repeating character is f
69

First non-repeating character is f
67
First non-repeating character is f
2

 

First non-repeating character is f
72
First non-repeating character is f
1
First non-repeating character is f
74

First non-repeating character is f
72
First non-repeating character is f
2

First non-repeating character is f
77
First non-repeating character is f
78

 

First non-repeating character is f
77
First non-repeating character is f
6

First non-repeating character is f
72
First non-repeating character is f
9

First non-repeating character is f
67
First non-repeating character is f
9

First non-repeating character is f
67using3

First non-repeating character is f
_9

 

First non-repeating character is f
_88

First non-repeating character is f
89
First non-repeating character is f
92

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)
Ruang Bantu. O(1)

Karakter non-berulang pertama menggunakan HashMap dan dua traversal string

Idenya adalah untuk menemukan frekuensi semua karakter dalam string dan memeriksa karakter mana yang memiliki frekuensi satuan

Bagaimana Anda menemukan karakter pertama yang tidak berulang dalam sebuah string?

Program Java untuk Menemukan Pertama karakter yang tidak berulang dalam sebuah String .
Metode 1 – Menggunakan metode indexOf() dan lastIndexOf()
Metode 2 – Metode ini membangun larik frekuensi
Metode 3 – Metode ini menggunakan Linked Hashmap
Metode 4 – Metode ini menggunakan Set dan ArrayList
Metode 5 – Metode ini menggunakan Java 8

Bagaimana menemukan kemunculan pertama karakter unik dalam string JavaScript?

Metode indexOf() mengembalikan posisi kejadian pertama dari sebuah nilai dalam sebuah string. Metode indexOf() mengembalikan -1 jika nilainya tidak ditemukan. Metode indexOf() peka terhadap huruf besar-kecil.

Bagaimana Anda menemukan karakter yang tidak berulang dalam sebuah string?

Karakter yang tidak berulang adalah karakter yang ada dalam string hanya sekali. Untuk menemukan karakter yang tidak berulang dalam sebuah string, kita akan menggunakan satu loop for untuk menghitung frekuensi setiap karakter dan mencetak karakter yang memiliki jumlah frekuensi satu menggunakan loop for lainnya.

Bagaimana menemukan karakter berulang pertama dalam string JavaScript?

Anda dapat menggunakan. indexOf() dan. lastIndexOf() untuk menentukan apakah suatu indeks diulang . Artinya, jika kemunculan karakter pertama juga merupakan kemunculan terakhir, maka Anda tahu itu tidak berulang.