List<Color> (@NotNull Collection<? extends RefGroup> groups) { LinkedHashMap<Color, Integer> usedColors = new LinkedHashMap<>(); for (RefGroup group : groups) { List<Color> colors = group.getColors(); for (Color color : colors) { Integer count = usedColors.get(color); if (count == null) count = 0; usedColors.put(color, count + 1); } } List<Color> result = new ArrayList<>(); for (Map.Entry<Color, Integer> entry : usedColors.entrySet()) { result.add(entry.getKey()); if (entry.getValue() > 1) { result.add(entry.getKey()); } } return result; }