How to find multiples of the same integer in an arraylist?

My problem is as follows. I have an arraylist of integers. Arraialist contains 5 chains, for example [5,5,3,3,9] or, possibly, [2,2,2,2,7]. Many of the arraylists have duplicate values, and I'm not sure how to calculate how many of each of the values ​​exist.

The problem is how to find duplicate values ​​in the arraylist and calculate how much exactly this duplicate is. In the first example [5,5,3,3,9] there are 2 5 and 2 3. The second example [2,2,2,2,7] will be only 4 2. The information I want to find is if there are some duplicates, how many of them are and which specific whole has been duplicated.

I'm not too sure how to do this in java.

Any help would be greatly appreciated. Thank.

+5
source share
6 answers

For me, the easiest answer would be to use a method Collections.frequency. Something like that:

// Example ArrayList with Integer values
ArrayList<Integer> intList = new ArrayList<Integer>();
intList.add(2);
intList.add(2);
intList.add(2);
intList.add(2);
intList.add(7);

Set<Integer> noDupes = new HashSet<Integer>();
noDupes.addAll(intList); // Remove duplicates

for (Integer i : noDupes) {
    int occurrences = Collections.frequency(intList, i);
    System.out.println(i + " occurs " + occurrences + " times.");
}

If you want, you can match each Integerwith its number of occurrences:

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Integer i : noDupes) {
    map.put(i, Collections.frequency(intList, i));
}
+6
source

Two spring algorithms for the mind.

Sort ( Collections.sort). Then try again to search for false errors.

Iterate by counting the quantity in Map<Integer,Integer>(or Map<Integer,AtomicInteger>for a variable account). This is a little ugly.

In any case, coding should be an instructive exercise. I propose to do both.

+5
source

, , , @Tom:

package playground.tests;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import junit.framework.TestCase;

public class DupeCounterTest extends TestCase {

    public void testCountDupes() throws Exception {
        int[] array = new int[] { 5, 5, 3, 3, 9 };
        assertEquals("{3=2, 5=2}", countDupes(array).toString());
    }

    private Map<Integer, AtomicInteger> countDupes(int[] array) {
        Map<Integer, AtomicInteger> map = new HashMap<Integer, AtomicInteger>();
        // first create an entry in the map for every value in the array
        for (int i : array)
            map.put(i, new AtomicInteger());
        // now count all occurrences
        for (int i : array)
            map.get(i).addAndGet(1);
        // now get rid of those where no duplicate exists
        HashSet<Integer> discards = new HashSet<Integer>();
        for (Integer i : map.keySet())
            if (map.get(i).get() == 1)
                discards.add(i);
        for (Integer i : discards) 
            map.remove(i);
        return map;
    }

}
+3

Hashmap ,

  • Hashmap int
  • Hashmap - .

, , , , 1 , . Hashmap , > 1.

+1

List Map . , .

+1

, , Multiset guava/google-collections. , , List, , ( ). :

Multiset<Integer> multiset = HashMultiset.create(list);
int count = multiset.count(3); // gets the number of 3s that were in the list

, , Map<Integer,AtomicInteger> .

0

All Articles