Easy Tutorial
❮ Java Arraylist Indexof Thread Status ❯

Java Example - Finding Duplicate Elements in an Array

Java Examples

The following example demonstrates how to find duplicate elements in a Java array:

Main.java File

public class MainClass {
    public static void main(String[] args) 
    {
        int[] my_array = {1, 2, 5, 5, 6, 6, 7, 2, 9, 2};
        findDupicateInArray(my_array);

    }

    public static void findDupicateInArray(int[] a) {
        int count=0;
        for(int j=0;j<a.length;j++) {
            for(int k =j+1;k<a.length;k++) {
                if(a[j]==a[k]) {
                    count++;
                }
            }
            if(count==1)
               System.out.println( "Duplicate Element : " +  a[j] );
            count = 0;
        }
    }
}

The output of the above code is:

Duplicate Element : 5
Duplicate Element : 6
Duplicate Element : 2

Java Examples

❮ Java Arraylist Indexof Thread Status ❯