например.
Код: Выделить всё
[5, 5, 2, 4, 2]
Код: Выделить всё
public class Item {
int id;
public Item(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
Код: Выделить всё
public class DuplicateItems {
public static int count(List items) {
int count = 0;
if (items.size() == 0) {
return 0;
}
items.sort(Comparator.comparingInt(Item::getId));
Map resultMap = new HashMap();
items.forEach(e -> resultMap.put(e, resultMap.getOrDefault(e, 0L) + 1L));
System.out.println(resultMap.size());
return count;
}
private static List convertToList(int[] values) {
List items = new ArrayList();
for (int num : values) {
items.add(new Item(num));
}
return items;
}
public static void main(String[] args) {
int[] itemsArray = {5, 5, 2, 4, 2};
List items = convertToList(itemsArray);
int duplicateCount = count(items);
System.out.println("Duplicate Count: " + duplicateCount);
}
}
Когда я запускаю программу, она говорит следующее:
< р>
Код: Выделить всё
Duplicate Count: 5Подробнее здесь: https://stackoverflow.com/questions/596 ... d-on-field
Мобильная версия