w3resource logo


Java Programming Exercies

Java Array Exercises: Find the duplicate values of an array of integer values

Secondary Nav

Java Array: Exercise-12 with Solution

Write a Java program to find the duplicate values of an array of integer values.

Java Code:

import java.util.Arrays; 
public class Exercise12 {
  public static void main(String[] args) 
    {
        int[] my_array = {1, 2, 5, 5, 6, 6, 7, 2};
 
        for (int i = 0; i < my_array.length-1; i++)
        {
            for (int j = i+1; j < my_array.length; j++)
            {
                if ((my_array[i] == my_array[j]) && (i != j))
                {
                    System.out.println("Duplicate Element : "+my_array[j]);
                }
            }
        }
    }    
}

Java Practice online :

import java.util.Arrays; 
public class Exercise12 {
  public static void main(String[] args) 
    {
        int[] my_array = {1, 2, 5, 5, 6, 6, 7, 2};
 
        for (int i = 0; i < my_array.length-1; i++)
        {
            for (int j = i+1; j < my_array.length; j++)
            {
                if ((my_array[i] == my_array[j]) && (i != j))
                {
                    System.out.println("Duplicate Element : "+my_array[j]);
                }
            }
        }
    }    
}

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.