Skip to content
Image
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Java Hashset problem solution

Image YASH PAL, 31 July 202416 January 2026

HackerRank Java Hashset problem solution – In this HackerRank java Hashset problem in java programming language 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 a number of unique pairs you currently have.

HackerRank Java Hashset problem solution

HackerRank Java HashSet problem solution.

import java.io.*;
import java.util.*;


class Pair<A, B> {
    private A first;
    private B second;

    public Pair(A first, B second) {
    	super();
    	this.first = first;
    	this.second = second;
    }

    public int hashCode() {
    	int hashFirst = first != null ? first.hashCode() : 0;
    	int hashSecond = second != null ? second.hashCode() : 0;

    	return (hashFirst + hashSecond) * hashSecond + hashFirst;
    }

    public boolean equals(Object other) {
    	if (other instanceof Pair) {
    		Pair otherPair = (Pair) other;
    		return 
    		((  this.first == otherPair.first ||
    			( this.first != null && otherPair.first != null &&
    			  this.first.equals(otherPair.first))) &&
    		 (	this.second == otherPair.second ||
    			( this.second != null && otherPair.second != null &&
    			  this.second.equals(otherPair.second))) );
    	}

    	return false;
    }

    public String toString()
    { 
           return "(" + first + ", " + second + ")"; 
    }

    public A getFirst() {
    	return first;
    }

    public void setFirst(A first) {
    	this.first = first;
    }

    public B getSecond() {
    	return second;
    }

    public void setSecond(B second) {
    	this.second = second;
    }
}
public class Solution {

    static private final String INPUT = "in";  
    static private final String OUTPUT = "out";  
  
    public static void main(String[] args) 
    {
		
	  FileInputStream instream = null;  
      PrintStream outstream = null;  
     
      try {  
          instream = new FileInputStream(INPUT);  
          outstream = new PrintStream(new FileOutputStream(OUTPUT));  
          //System.setIn(instream);  
          //System.setOut(outstream);  
      } catch (Exception e) {  
          System.err.println("Error Occurred.");  
      }  
        Set setA = new HashSet();
        Scanner scan = new Scanner(System.in);
        int t=scan.nextInt();
        for(int i=0;i<t;i++)
        {
			String a=scan.next();
			String b=scan.next();
			Pair P=new Pair(a,b);
			setA.add(P);
			System.out.println(setA.size());
			//System.out.println(a+" "+b);
		}
			
        
    }
}

Second solution

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        Set<Pair> pairs = new HashSet<>();
        final int N = s.nextInt();
        
        for(int i = 0; i < N; i++) {
            String first = s.next();
            String second = s.next();
            
            pairs.add(new Pair(first, second));
            System.out.println(pairs.size());
        }       
    }
    
    public static class Pair {
        String first, second;
        Pair(String first, String second) {
            this.first = first;
            this.second = second;
        }
        
        @Override public int hashCode() {
            return first.hashCode() + second.hashCode() * 4 % 4;
        }

        @Override public boolean equals(Object other) {
            if (other == this) {
                return true;
            }
            if (!(other instanceof Pair)) {
                return false;
            }

            Pair otherPair = (Pair) other;
            return (first.equals(otherPair.first) && second.equals(otherPair.second))
                || (first.equals(otherPair.second) && second.equals(otherPair.first));
        }
        
    }
}

The solution in java8 programming

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

 public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int t = s.nextInt();
        String [] pair_left = new String[t];
        String [] pair_right = new String[t];
        
        for (int i = 0; i < t; i++) {
            pair_left[i] = s.next();
            pair_right[i] = s.next();
        }

HashSet<String> pairs = new HashSet<String>(t);

    for(int i = 0; i < t; i++)
    {
        pairs.add("(" + pair_left[i] + ", " + pair_right[i] + ")" );
        System.out.println(pairs.size());        
    }
   }
}

Second solution

import java.io.*;
import java.util.*;

public class Solution {

   	public static void main(String[] args) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));
		int x = Integer.valueOf(reader.readLine());
		HashSet<String> set = new HashSet<String>();
		while (x-- > 0) {
			set.add(reader.readLine());
			System.out.println(set.size());
		}
	}
}
coding problems solutions Hackerrank Problems Solutions Java solutions HackerRankjava

Post navigation

Previous post
Next post

Leave a Reply

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

HackerRank Java Solutions
Welcome to Java! problem solution
Java Stdin and Stdout I problem solution
Java If-Else problem solution
Java Stdin and Stdout II problem solution
Java Output Formatting problem solution
Java Loops I problem solution
Java Loops II problem solution
Java Datatypes problem solution
Java End-of-file problem solution
Java Static Initializer Block solution
Java Int to String problem solution
Java Date and Time problem solution
Java Currency Formatter problem solution
Java Strings Introduction problem solution
Java Substring problem solution
Java Substring Comparisons problem solution
Java String Reverse problem solution
Java Anagrams problem solution
Java String Tokens problem solution
Pattern Syntax Checker solution
Java Regex problem solution
Java Regex 2 — Duplicate Words solution
Valid Username Regular Expression solution
Java ArrayList problem solution
Java BigDecimal problem solution
Java Primality Test problem solution
Java BigInteger problem solution
Java 1D Array problem solution
Java 2D Array problem solution
java Subarray problem solution
Java Arraylist problem solution
Java 1D Array (Part 2) problem solution
Java List problem solution
Java Map problem solution
Java Stack problem solution
Java Hashset problem solution
Java Generics problem solution
Java Comparator problem solution
Java Sort problem solution
Java Dequeue problem solution
Java BitSet problem solution
Java Priority Queue problem solution
Java Inheritance I problem solution
Java Inheritance II problem solution
Java Abstract Class problem solution
Java Interface problem solution
Java Method Overriding problem solution
Java Method Overriding 2 (Super Keyword) solution
Java Instanceof keyword problem solution
Java Iterator problem solution
Java Exception Handling (Try-catch) solution
Java Exception Handling problem solution
Java Varargs — Simple Addition solution
Java Reflection — Attributes problem solution
Can You Access? problem solution
Prime Checker problem solution
Java Factory Pattern problem solution
Java Singleton Pattern problem solution
Java Visitor Pattern problem solution
Java Annotations problem solution
Covariant Return Types solution
Java Lambda Expressions problem solution
Java MD5 problem solution
Java SHA-256 problem solution

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes
Advertisement