![]() |
Problem statement: We need to find and print unique pairs among all given pairs. Generally, if we have to find the unique numbers among all given numbers then we just put all numbers in HashSet and print them. Let us consider a sample illustration to interpret the problem statement better. Suppose we have three pairs be customly they are (3, 5), (4, 5), (3, 5). Now if we put it in the set then the output will be. Output: (3, 5), (4, 5), (3, 5)
HashSet<Pair> set = new HashSet<>(); From the example, we can clearly see that duplicate pairs are still present. This is because for the set, Pair is the same type of object and it can’t differentiate between them. So to overcome this problem we can just make a string of indexes and values of Pairs and store it in Set in form of String and then print it and you can get all unique pairs. Example Java
Output
(5, 10) (2, 3) (3, 4) Output explanation: So from the code and output, you can clearly see that duplicate pairs are deleted and we can get all unique pairs without overriding the hashCode() method and equals() method. |
Reffered: https://www.geeksforgeeks.org
Java |
Related |
---|
![]() |
![]() |
![]() |
![]() |
|
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |