You are viewing a single comment's thread. Return to all comments →
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(); } // long startTime = System.nanoTime(); //Write your code here // HashMap cost 375400 (faster than hashset) HashMap<String,String> hm = new HashMap<>(); for (int i = 0; i<pair_left.length;i++){ hm.put(pair_left[i]+" "+pair_right[i],null ); System.out.println(hm.size()); } // HashSet cost 408040 (longer than hashmap) // HashSet<String> hs = new HashSet<>(); // for (int i=0;i<pair_right.length;i++){ // hs.add(pair_left[i]+" "+pair_right[i]); // System.out.println(hs.size()); // } // long elapsedNanos = System.nanoTime() - startTime; // System.out.println(elapsedNanos); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Java Hashset
You are viewing a single comment's thread. Return to all comments →