Count Duplicate Pairs of Integer in given array
Input: Integer array with 0 or more repeated values.
Output: Total number of duplicate numbers present in the given array
The use of Hash set can be done in order to solve this by traversing each element and adding it into the set, later on they are checked for repetitive addition (i.e. we make sure that the numbers were not included already!)
Use Hashset(Not HashMap), as we don't care the real count.
Read full article from Count Duplicate Pairs of Integer in given array
Input: Integer array with 0 or more repeated values.
Output: Total number of duplicate numbers present in the given array
The use of Hash set can be done in order to solve this by traversing each element and adding it into the set, later on they are checked for repetitive addition (i.e. we make sure that the numbers were not included already!)
Use Hashset(Not HashMap), as we don't care the real count.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| import java.util.HashSet; public class CountDuplicates { static int countDuplicates( int [] numbers) { HashSet<Integer> set = new HashSet<Integer>(); HashSet<Integer> duplicateSet = new HashSet<Integer>(); for ( int i= 0 ;i<numbers.length;i++){ if (set.contains(numbers[i])){ duplicateSet.add(numbers[i]); } else { set.add(numbers[i]); } } return duplicateSet.size(); } |