Count Duplicate Pairs of Integer in a given array (Hacker Rank Interview Questions)

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 the 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.



Below is the code:

The output of the above code:

You can have above code from GIT.


Leave a Reply

Your email address will not be published. Required fields are marked *