You are viewing a single comment's thread. Return to all comments →
My solution, in Java 8, which passes all test cases. The code is fairly simple & self-explanatory.
// Complete the freqQuery function below. static List<Integer> freqQuery(List<List<Integer>> queries) { List<Integer> q3Results = new ArrayList<>(); Map<Integer,Integer> intElemFreqMap = new LinkedHashMap<>(); for (int qIdx = 0 ; qIdx < queries.size() ; qIdx++) { assert(queries.get(qIdx).size() == 2); int opType = queries.get(qIdx).get(0); int opVal = queries.get(qIdx).get(1); // System.err.println("opType=>" + opType + "<"); // System.err.println("opVal=>" + opVal + "<"); switch(opType) { case 1: intElemFreqMap.put(opVal, intElemFreqMap.getOrDefault(opVal, 0) + 1); break; case 2: if (intElemFreqMap.containsKey(opVal) && intElemFreqMap.get(opVal) >= 1) { intElemFreqMap.put(opVal, intElemFreqMap.get(opVal) - 1); } break; case 3: if (intElemFreqMap.containsValue(opVal)) { q3Results.add(1); } else { q3Results.add(0); } break; default: System.err.println("Unknown OpType=>" + opType + "<"); } } return q3Results; }
Seems like cookies are disabled on this browser, please enable them to open this website
Frequency Queries
You are viewing a single comment's thread. Return to all comments →
My solution, in Java 8, which passes all test cases. The code is fairly simple & self-explanatory.