﻿ Java exercises: Find the duplicate values of an array of integer values - w3resource

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

Java Array: Exercise-12 with Solution

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

Sample Solution:

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]);
}
}
}
}
}
```
```

Sample Output:

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

Java Code Editor:

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