Find all duplicates in an array in Java
This post will discuss how to report all duplicates in an array in Java.
1. Using a Set
The idea is to iterate through the array and keep track of the encountered items in a Set. If an element is seen before, mark it as duplicate and report all duplicates at the end of the loop. This can be easily done using Java 8 Stream:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; public class Main { public static Set<Integer> findDuplicates(int[] arr) { Set<Integer> seen = new HashSet<>(); return Arrays.stream(arr).filter(i -> !seen.add(i)) .boxed().collect(Collectors.toSet()); } public static void main(String[] args) { int[] values = {4, 3, 2, 3, 1, 4, 5}; Set<Integer> duplicates = findDuplicates(values); System.out.println(duplicates); } } |
Output:
[3, 4]
Here’s a version without using streams:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.HashSet; import java.util.Set; public class Main { public static Set<Integer> findDuplicates(int[] arr) { Set<Integer> seen = new HashSet<>(); Set<Integer> duplicates = new HashSet<>(); for (int i: arr) { if (!seen.add(i)) { duplicates.add(i); } } return duplicates; } public static void main(String[] args) { int[] values = {4, 3, 2, 3, 1, 4, 5}; Set<Integer> duplicates = findDuplicates(values); System.out.println(duplicates); } } |
Output:
[3, 4]
2. Using a List
Another solution is to convert the array to a list and filter duplicates in the list using Stream API. The following code uses the Collections.frequency() method to get the frequency of each element in the collection.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class Main { public static Set<Integer> findDuplicates(int[] arr) { List<Integer> nums = Arrays.stream(arr).boxed().collect(Collectors.toList()); return nums.stream().filter(i -> Collections.frequency(nums, i) > 1) .collect(Collectors.toSet()); } public static void main(String[] args) { int[] values = {4, 3, 2, 3, 1, 4, 5}; Set<Integer> duplicates = findDuplicates(values); System.out.println(duplicates); } } |
Output:
[3, 4]
3. Using a Frequency Map
The above implementation is not efficient, as it is getting the frequency of each element. We can improve its performance by creating a frequency map and then filtering the values having a frequency more than 1.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; public class Main { public static Set<Integer> findDuplicates(int[] arr) { Map<Integer, Long> frequencies = Arrays.stream(arr).boxed() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); return frequencies.keySet().stream() .filter(key -> frequencies.get(key) > 1) .collect(Collectors.toSet()); } public static void main(String[] args) { int[] values = {4, 3, 2, 3, 1, 4, 5}; Set<Integer> duplicates = findDuplicates(values); System.out.println(duplicates); } } |
Output:
[3, 4]
Here’s a version without using streams:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class Main { public static Set<Integer> findDuplicates(int[] arr) { Map<Integer, Long> frequencies = new HashMap<>(); for (int i: arr) { frequencies.merge(i, 1L, Long::sum); } Set<Integer> duplicates = new HashSet<>(); for (Integer key : frequencies.keySet()) { if (frequencies.get(key) > 1) { duplicates.add(key); } } return duplicates; } public static void main(String[] args) { int[] values = {4, 3, 2, 3, 1, 4, 5}; Set<Integer> duplicates = findDuplicates(values); System.out.println(duplicates); } } |
Output:
[3, 4]
That’s all about finding all duplicates in an array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)