In this HackerRank Java HashSet problem, you are given n pairs of string. 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.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Set;
import java.util.HashSet;


public class Solution
{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 64 * 1024);

        final Set<String> set = new HashSet<String>();

        final StringBuilder sb = new StringBuilder();
        final int T = Integer.parseInt(br.readLine().trim(), 10);
        for (int t = 0; t < T; t++) {
            final String S = br.readLine().trim();
            set.add(S);
            sb.append(set.size()).append('\n');
        }
        System.out.print(sb.toString());

        br.close();
        br = null;
    }
}