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.
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()); } } }