sclass LineComp_PairCounts { new MultiSet counts; MultiSetMap byCount = multiSetMap_innerTreeSet_outerRevTreeMap(); void addAll(Iterable l) { fOr (IntPair p : l) add(p); } void removeAll(Iterable l) { fOr (IntPair p : l) remove(p); } void add(int a, int b) { add(IntPair(a, b)); } void remove(int a, int b) { remove(IntPair(a, b)); } // increment pair's count void add(IntPair p) { int count = counts.get(p); byCount.remove(count, p); counts.add(p); byCount.put(count+1, p); } // decrement pair's count void remove(IntPair p) { int count = counts.get(p); if (count == 0) fail("Can't remove pair " + p); byCount.remove(count, p); counts.remove(p); if (count-- > 0) byCount.put(count, p); } IntPair mostPopularDuplicate() { ret toInt(firstKey(byCount)) < 2 ? null : firstValue(byCount); } int numberOfDuplicates() { ret counts.size()-l(byCount.get(1)); } int getCount(IntPair p) { ret counts.get(p); } }