Java 8 – Find duplicate count from Integer arrays ?

In this article, we will discuss how to find the desired result from the Integer[] arrays for the problem statement given below

1. Problem statement :

For the given integer array[] find the duplicate count in the specified-order below,

  • Highest count of duplicate-number should come first i.e.; decreasing-order of duplicate count
  • If there are 2 or more duplicate-number with same count then duplicate-number with higher-value should come first i.e.; again decreasing-order of duplicate numbers
  • If there are 2 or more duplicate-number with count as 1 then duplicate-number with highest-value should only be there and remaining numbers should be discarded

2. Example :

int arr[] = {7, 10, 11, 5, 2, 5, 5, 7, 11, 8, 9}

5 11 7 10

In the above integer array[],

  • Number 5 is repeated thrice therefore it should come first
  • Number 7 & Number 11 repeated twice but out of these 2 numbers 11 is greater than 7 therefore order should be 11 first and then 7
  • Numbers 2, 8, 9, 10 repeated only once therefore highest number should be considered discarding remaining numbers therefore Number 10 should be there at the very end in the final result

3. Solution proposed :

The solution proposed for the above problem statement is –

  • First, convert the given integer[] arrays to Map of integers (duplicate-number) and their count (duplicate-count)
  • Sort the converted map
    • first by decreasing-order of duplicate-count (VALUE)
    • and then by decreasing-order of duplicate-number (KEY)
  • Get the count of duplicate-number whose duplicate-count is greater/equal to 2
  • Finally, limit the duplicate-number based on the count as per the given problem statement and print final result to the console
  • Note:
    • duplicate-number is the number from integer[] arrays
    • duplicate-count is the number of times it is repeated/duplicated

4. Implementation for the problem statement :

CountDuplicates.java

package in.bench.resources.find.duplicate.count;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class CountDuplicates {

	// main() method
	public static void main(String[] args) {

		// test array[] with duplicates
		int arr[] = {7, 10, 11, 5, 2, 5, 5, 7, 11, 8, 9};


		// 1. convert array[] to Map of integers and their count
		Map<Integer, Long> duplicateCountMap = Arrays.stream(arr)
				.boxed()
				.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
		System.out.println("1. Duplicate Count Map = " + duplicateCountMap);


		// 2. sort map first by decreasing-order of duplicate-count (VALUE) 
		// and then by decreasing-order of duplicate number (KEY)
		LinkedHashMap<Integer, Long> duplicateCountSortedMap = duplicateCountMap
				.entrySet()
				.stream()
				.sorted(
						Map.Entry.<Integer, Long>comparingByValue().reversed()
						.thenComparing(Map.Entry.<Integer, Long>comparingByKey().reversed())
						)
				.collect(
						Collectors.toMap(
								Map.Entry::getKey, 
								Map.Entry::getValue, 
								(oldValue, newValue) -> oldValue, 
								LinkedHashMap::new)
						);
		System.out.println("2. Duplicate Count Sorted Map = " + duplicateCountSortedMap);


		// 3. get count of number whose duplicate-count is greater/equal to 2
		long duplicateCountGreaterThanEqualTo2 = duplicateCountSortedMap
				.entrySet()
				.stream()
				.filter(entry -> entry.getValue() >= 2)
				.count();
		System.out.println("3. Duplicate Count Greater Than or Equal To two = " 
				+ duplicateCountGreaterThanEqualTo2);


		// 4. limit based on the count and print to console
		System.out.print("4. Desired result = ");
		duplicateCountSortedMap
		.entrySet()
		.stream()
		.limit(duplicateCountGreaterThanEqualTo2 + 1)
		.forEach(entry -> System.out.print(entry.getKey() + " "));
	}
}

Output :

1. Duplicate Count Map = {2=1, 5=3, 7=2, 8=1, 9=1, 10=1, 11=2}
2. Duplicate Count Sorted Map = {5=3, 11=2, 7=2, 10=1, 9=1, 8=1, 2=1}
3. Duplicate Count Greater Than or Equal To two = 3
4. Desired result = 5 11 7 10 

Related Articles :

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to sort HashMap by its Values first and then by its Keys ?
Java 8 - Find employee count in each department ?