Cek Data Duplikat Dalam Array Dan List Pada JAVA

Berikut adalah cara untuk mengecek data yang duplikat atau lebih dari satu dalam sebuah array dan list pada JAVA.



int[] numbers = { 1, 5, 23, 2, 1, 6, 3, 01, 8, 12, 3, 2 };
Arrays.sort(numbers);

for(int i = 1; i < numbers.length; i++) {
   if(numbers[i] == numbers[i - 1]) {
       System.out.println("Duplicate Cara Pertama : " + numbers[i]);
   }
}


String[] withDuplicates = new String[] {"one","two","three","one"};
String[] withoutDuplicates = new String[] {
"one","two","three"};
Set tempSet = new HashSet();

for (String str : withDuplicates) {
     
if (!tempSet.add(str)) {
            System.out.println("
Duplicate Cara Kedua : " + str);
      }
}

List Add = new ArrayList();
Add.add("Liverpool");
Add.add("Chelsea");
Add.add("Liverpool");
Add.add("Arsenal");
Add.add("Milan");
Add.add("Juventus");
Add.add("Arsenal");

for (String str : Add) {
      if (!tempSet.add(str)) {
           System.out.println("Duplicate Cara Ketiga : " + str);
      }
}


Demikian contoh program dari saya, semoga bermanfaat. GBU.


Related Posts

Share on Google Plus

About Mr.J

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Posting Komentar