HackerRank Java- Hashset




You are given n pairs of strings. Two pairs (a, b) and (c, d) are identical if a=c and b=d. That also implies (a, b) is not same as (b, a). After taking each pair as input, you need to print number of unique pairs you currently have.

Complete the code in the editor to solve this problem.



  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6. public class Solution {
  7. public static void main(String[] args) {
  8. Scanner s = new Scanner(System.in);
  9. int t = s.nextInt();
  10. String [] pair_left = new String[t];
  11. String [] pair_right = new String[t];
  12. for (int i = 0; i < t; i++) {
  13. pair_left[i] = s.next();
  14. pair_right[i] = s.next();
  15. }
  16. HashSet<String> set = new HashSet(t);
  17. for (int i = 0; i < t; i++) {
  18. set.add(pair_left[i] + " " + pair_right[i]);
  19. System.out.println(set.size());
  20. }
  21. }
  22. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube