0

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"!

4
  • 2
    Duplicate of? Detect duplicate values in primitive Java array Commented 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" Commented Feb 22, 2020 at 16:14
  • what should be the output? Commented Feb 22, 2020 at 16:17
  • @GovindaSakhare an integer. Commented Feb 22, 2020 at 16:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
@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.
You solved my problem mate. thanks. +1 vote and accepted answer
1

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

Neat solution. I did the same thing, just with good old for loops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.