Detecting duplicate values ​​in a Java primitive array

I want to detect duplicate values ​​in a Java array. For instance:

int[] array = { 3, 3, 3, 1, 5, 8, 11, 4, 5 }; 

How can I get a specific duplicate entry and how many times does it occur?

+4
source share
7 answers

I will have Map<Integer, Integer> , where the first integer value number that appears in the array, and the second integer is count (the number of occurrences).

  • Run array.length in a loop
  • for each element of the array, execute map.containsKey(array[i]) . If there is a number on the map, increase that number (something like map.put(array[i], map.get(array[i]) + 1) . Otherwise, create a new entry on the map (for example, map.put(array[i], 1) .
  • Finally, iterate over the map and extract all the keys where the value is greater than 1.
+6
source

It seems to work for a data structure called multiset .

 Multiset<Integer> mp = HashMultiset.create(); mp.addAll(Arrays.asList(new Integer[] { 3, 3, 3, 1, 5, 8, 11, 4, 5 })); 

The standard JDK 6 is primitive and does not contain multiset . If you do not want to rewrite it, you can use a pre-existing library such as Google Guava libraries or Apache Commons.

For example, using Guava libraries you can

  for (Integer i : mp.elementSet()) System.out.println(i + " is contained " + mp.count(i) + " times."); 

And this will output:

 1 is contained 1 times. 3 is contained 3 times. 4 is contained 1 times. 5 is contained 2 times. 8 is contained 1 times. 11 is contained 1 times. 
+5
source

The answer depends on the range of numbers in your source array. If the range is small enough, you can select an array, skip your source and increase it in the index of your original number:

 int[] counts = new int[max_value + 1]; for (int n: array) { counts[n]++; } 

If your source array contains an unknown or too large range, you can create a Map and count in it:

 Map<Integer,Integer> counts = new HashMap<Integer,Integer>(); for (Integer n: array) { if (counts.containsKey(n)) { counts.put(n, counts.get(n) + 1); } else { counts.put(n, 1); } } 

NB. I typed the above without the help of the JVM, getting rid of the printing houses was left as an exercise for the reader :-)

+4
source
 public class Duplicate { public static void main(String[] arg) { int[] array = {1, 3, 5, 6, 2, 3, 6, 4, 3, 2, 1, 6, 3}; displayDuplicate(array); } static void displayDuplicate(int[] ar) { boolean[] done = new boolean[ar.length]; for(int i = 0; i < ar.length; i++) { if(done[i]) continue; int nb = 0; for(int j = i; j < ar.length; j++) { if(done[j]) continue; if(ar[j] == ar[i]) { done[j] = true; nb++; } } System.out.println(ar[i] + " occurs " + nb + " times"); } } } 
+3
source
 import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class DuplicatedValuesInArray { public static void main(String args[]) { int[] array = { 3, 3, 3, 1, 5, 8, 11, 4, 5 }; Map<Integer, Integer> map= new HashMap<Integer, Integer>(); for(int i=0;i<array.length;i++) { if(map.containsKey(array[i])) map.put(array[i],map.get(array[i]) + 1); else map.put(array[i], 1); } for (Integer i : map.keySet()) { System.out.println(i + " is contained " + map.get(i) + " times."); } } } 
+3
source

assign a counter for the first step, then you can associate them with another array, assigning an index to each number, and then, if your number is duplicated, increase the counter ...

0
source

Sort the array, then either scan it or Arrays.binarySearch + scan in any direction. Due to the much smaller number of distributions and without packaging, this can be faster, especially on large arrays.

0
source

Source: https://habr.com/ru/post/1344395/


All Articles