You are viewing a single comment's thread. Return to all comments →
JAVA 8
The below code was failing test 11 untill I switched from HashMap to LinkedHashMap. Now it passes all tests.
static List freqQuery(List> queries) {
List<Integer> result = new ArrayList<>(); Map<Integer, Integer> counts = new LinkedHashMap<>(); queries.forEach(query ->{ Integer operation = query.get(0); Integer value = query.get(1); Integer current = counts.getOrDefault(value, 0); switch (operation) { case 1: counts.put(value, current+1); break; case 2: if (current > 0) counts.put(value, current-1); break; case 3: boolean count = counts.containsValue(value); result.add(count?1:0); } }); return result; }
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 →
JAVA 8
The below code was failing test 11 untill I switched from HashMap to LinkedHashMap. Now it passes all tests.
static List freqQuery(List> queries) {