int (@NotNull Bag bag1, @NotNull Bag bag2) { Bag toIterate = (bag1.getBag().size() > bag2.getBag().size()) ? bag2 : bag1; Bag toProcess = (bag1.getBag().size() > bag2.getBag().size()) ? bag1 : bag2; int cardinality = 0; for (Object2IntMap.Entry<String> entry : toIterate.getBag().object2IntEntrySet()) { cardinality += Math.min(toProcess.getBag().getInt(entry.getKey()), toIterate.getBag().getInt(entry.getKey())); } return cardinality; }