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_
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.
Memasukkan. “geeksforgeeks”
Keluaran. F
Penjelasan. Karena 'f' adalah karakter pertama dalam string yang tidak berulangMemasukkan. “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 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
using
6
Jawa
using
_7
________426______8 using
9
namespace
0 namespace
1 namespace
2
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
Python3
First 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
Javascript
First 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
using
6
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;
namespace
6
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
Jawa
First non-repeating character is f_00
using
8
First non-repeating character is f02
namespace
1 namespace
2
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
Python3
First 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
using
First non-repeating character is f97
namespace
0 namespace
5 namespace
1 namespace
2
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
Javascript
First 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]
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 frekuensiMetode 3 – Metode ini menggunakan Linked HashmapMetode 4 – Metode ini menggunakan Set dan ArrayListMetode 5 – Metode ini menggunakan Java 8Bagaimana 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.