You are viewing a single comment's thread. Return to all comments →
Simple Java Solution
public static int pairs(int k, List arr) { int count = 0; HashMap map = new HashMap<>(); for(int i : arr) { map.put(i, map.getOrDefault(i, 1)+1); } for(Map.Entry e: map.entrySet()) { int sum = e.getKey() - k; if(map.containsKey(sum)) { count++; } } return count; }
}
Pairs
You are viewing a single comment's thread. Return to all comments →
Simple Java Solution
public static int pairs(int k, List arr) { int count = 0; HashMap map = new HashMap<>(); for(int i : arr) { map.put(i, map.getOrDefault(i, 1)+1); } for(Map.Entry e: map.entrySet()) { int sum = e.getKey() - k; if(map.containsKey(sum)) { count++; } } return count; }
}