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!)
Step 1: Create 2 hash sets. 1 is for all the elements and the other is for the duplicate elements.
Step 2: Loop over each element in the given array
Now, for each element:
a. Check whether element is in the Hash set
b. If yes, then add it to duplicate set
c. If no, then add it to normal hash set
Step 3: output the size of duplicate hash set
Below is the code in java for the above problem.
You can have below code from GIT.
Read full article from Count Duplicate Pairs of Integer in given array
No comments:
Post a Comment