Java – Find, Count and Remove Duplicate Elements from Array

Learn to find, count and remove all the duplicate elements from an array in Java using techniques such as Streams, Map and Set from the Collections framework.

We will be using the following array of Integer values. The logic remains the same for other datatypes as well.

1. Using Stream and Map

The Stream API provides excellent ways to process elements from any Collection or array. We can create a Map of all distinct elements as Map key and their number of occurrences in the array as Map value.

Map<Integer, Long> map = Arrays.stream(numArray)
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));  //{1=2, 2=1, 3=2, 4=1, 5=2}

We can also iterate over the array elements and create a similar Map.

Map<Integer, Long> map = new HashMap<>();

for (int i : numArray) {
  if (map.containsKey(i)) { //this element is in the map already
    map.put(i, map.get(i) + 1);
  } else { //found a new element
    map.put(i, 1L);
  }
}

Now we can use the Map keys and values to count duplicates, and even collect the duplicate and unique elements into a new array.


long duplicateCount = map.keySet()
  .stream()
  .filter(k -> map.get(k) > 1)
  .collect(Collectors.counting());

System.out.println("Count of duplicate elements : " + duplicateCount);   

Integer[] duplicateElementsArray = map.keySet()
  .stream()
  .filter(k -> map.get(k) > 1)
  .toArray(Integer[]::new);

System.out.println("Duplicate elements in the array : " + Arrays.toString(duplicateElementsArray));

Integer[] uniqueElementsArray = map.keySet()
  .stream()
  .filter(k -> map.get(k) == 1)
  .toArray(Integer[]::new);

System.out.println("Unique elements in the array : " + Arrays.toString(uniqueElementsArray));

The program output.

Count of duplicate elements : 3
Duplicate elements in the array : [1, 3, 5]
Unique elements in the array : [2, 4]

2. Using Stream and Set

Java Set class stores only the distinct elements. We can use this feature to find the distinct elements in the array and then find unique and duplicate elements using the simple add and remove operations.

The following code tries to add all elements from the array into the HashSet. The add() operation returns false for duplicate elements that are already present in the Set.

Integer[] numArray = new Integer[]{1, 2, 3, 4, 5, 1, 3, 5};

Set<Integer> distinctElementsSet = new HashSet<>();
Integer[] duplicateElementsArray = Arrays.stream(numArray)
    .filter(e -> !distinctElementsSet.add(e))
    .toArray(Integer[]::new);

System.out.println("Duplicate elements in the array : " + Arrays.toString(duplicateElementsArray));  //1, 3, 5]

int duplicateCount = duplicateElementsArray.length;

System.out.println("Count of duplicate elements : " + duplicateCount);   //3

If we remove all the duplicate elements from the Set, it will contain only the unique elements.

distinctElementsSet.removeAll(Arrays.asList(duplicateElementsArray));
Integer[] uniqueElementsArray = distinctElementsSet.toArray(Integer[]::new);

System.out.println("Unique elements in the array : " + Arrays.toString(uniqueElementsArray));  //[2, 4]

3. Conclusion

In this short Java tutorial, we learned two different ways to find and count duplicate elements in a Java array. We also learned to collect and print the duplicate and the unique elements in a new array.

Happy Learning !!

Sourcecode Download

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode