Java finding duplicates in an array

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.

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 map = Arrays.stream(numArray) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); //

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

Map 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));
Count of duplicate elements : 3 Duplicate elements in the array : [1, 3, 5] Unique elements in the array : [2, 4]

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.

Читайте также:  Javascript объекты и наследование

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[]; Set 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]

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.

Источник

Оцените статью