You are viewing a single comment's thread. Return to all comments →
From my HackerRank solutions.
Use a HashSet instead of a HashMap for simpler code.
import java.util.Scanner; import java.util.HashSet; public class Solution { public static void main(String[] args) { /* Read some input */ Scanner scan = new Scanner(System.in); int n = scan .nextInt(); /* Count pairs */ HashSet<Integer> set = new HashSet<>(); int pairs = 0; for (int i = 0; i < n; i++) { int cost = scan.nextInt(); if (set.contains(cost)) { set.remove(cost); pairs++; } else { set.add(cost); } } /* Print output */ scan.close(); System.out.println(pairs); } }
Let me know if you have any questions.
Seems like cookies are disabled on this browser, please enable them to open this website
Sales by Match
You are viewing a single comment's thread. Return to all comments →
Java solution - passes 100% of test cases
From my HackerRank solutions.
Use a HashSet instead of a HashMap for simpler code.
Let me know if you have any questions.