I have an array of integers like = 3,1,2,3,1 ; and I just want to count how many duplicates this array has. for example this array has 4 duplicates now. input is the array and the output will be number of duplicating elements. is there any methods or classes for it? can you help me to implement my own? Note : I'm not looking for number of occurrence of a specified element. I just want to know how many duplicates this array has at all. for example I said it has 4 duplicates for the mentioned array. 2 of them are "3" and 2 of them are "1"!
-
2Duplicate of? Detect duplicate values in primitive Java arrayGovinda Sakhare– Govinda Sakhare2020-02-22 16:11:38 +00:00Commented Feb 22, 2020 at 16:11
-
@GovindaSakhare no its not. I don't want to count how many times each element repeated and print it. I just need the repetition of all elements. for example in the post I said we have 4 duplicates. 2 of them are "3" and 2 of them are " 1"Semiron– Semiron2020-02-22 16:14:08 +00:00Commented Feb 22, 2020 at 16:14
-
what should be the output?Govinda Sakhare– Govinda Sakhare2020-02-22 16:17:20 +00:00Commented Feb 22, 2020 at 16:17
-
@GovindaSakhare an integer.Semiron– Semiron2020-02-22 16:19:53 +00:00Commented Feb 22, 2020 at 16:19
Add a comment
|
2 Answers
Well a map can be used to store then numbers and the amount of times they appeared:
Map<Integer,Integer> map = new HashMap<>();
for(int i: numbers) { //suppose you have the numbers in 'numbers' array
if(map.containsKey(i) { //this number is in the map already
map.put(i, map.get(i)+1);
} else { //we didnt see this number before
map.put(i, 1);
}
}
//now we loop the map to sum the repeating numbers
int sum = 0;
for(int amount: map.values()) {
if(amount > 1) sum += amount;
}
An advantage of this method is that you know exactly how many of each numbers the are in the array. You may need that info later.
3 Comments
Semiron
Hello, thanks for your help. but I'm not looking for number of occurrence of all elements. I just want to know if an array has a repeated value, how many repeated values it has at all. I edited the first post too
Gtomika
@Semiron This does just that. The total amount of repeating elements will be in the sum variable. If the sum is 0, that means the array had no repeating elements.
Semiron
You solved my problem mate. thanks. +1 vote and accepted answer
The easiest way is to use a map. The array is converted to a stream of integers. The groupingBy collector uses the integers as keys to the map. The counting collector counts the number or occurrences of those integers.
int[] nums = { 1, 20, 2, 3, 1, 2, 3, 2, 221, 20 };
Map<Integer, Long> dups = Arrays.stream(nums)
.boxed()
.collect(Collectors.groupingBy(a -> a, Collectors.counting()));
To print it you can do this for each key and value.
dups.forEach((k,v)-> System.out.println(k + " -> " + v));
To count the total duplicates, take the map and sum those values > 1.
long sum = dups.values()
.stream()
.filter(a-> a > 1)
.count();
System.out.println("There are " + sum + " duplicates in all");
1 Comment
Gtomika
Neat solution. I did the same thing, just with good old for loops.