Count duplicate values in list

3

I have ArrayList with multiple numbers inside it. I need to identify how many times each number appears and then eliminate redundancies. I was able to eliminate redundancies, but I'm having great problems in identifying how many of them each has.

What you can do so far:

private List<Calculos> RemoverDuplicados()
{
    aux = new ArrayList<Calculos>();
    ids = new ArrayList<Integer>();

    int[] intArray = new int[aux.size()];

    //separa os cálculos repetidos e envia para a lista auxiliar;
    for(Calculos i : calculosItems)
    {
        //se a variável 'ids' não tiver o valor do ID do produto, eu adiciono aqui
        if(!ids.contains(Integer.valueOf(i.getId_produto())))
        {
            aux.add(i);
            ids.add(Integer.valueOf(i.getId_produto()));
        }
    }

    for(Calculos i : aux){
        Log.d(TAG,"ID: " + i.getId_produto() + " possui: ? calculos");;
    }

    return aux;
}

These are the values (which are inside the database):

2
1
2
2
2
2
2
1
3
3
3
3
4
5
5
6
6
6

The current output is:

D/﹕ ~~ Calculos depois:
05-16 19:48:23.586  28643-28643/com.financeiro.coolkey.financeiro_2 D/﹕ ID: 2 possui: ? calculos
05-16 19:48:23.586  28643-28643/com.financeiro.coolkey.financeiro_2 D/﹕ ID: 1 possui: ? calculos
05-16 19:48:23.586  28643-28643/com.financeiro.coolkey.financeiro_2 D/﹕ ID: 3 possui: ? calculos
05-16 19:48:23.586  28643-28643/com.financeiro.coolkey.financeiro_2 D/﹕ ID: 4 possui: ? calculos
05-16 19:48:23.586  28643-28643/com.financeiro.coolkey.financeiro_2 D/﹕ ID: 5 possui: ? calculos
05-16 19:48:23.586  28643-28643/com.financeiro.coolkey.financeiro_2 D/﹕ ID: 6 possui: ? calculos

Instead of ? it should show me the number of calculations, for example: ID 1 possui 2 calculos .

Can anyone help me, please?

    
asked by anonymous 17.05.2015 / 00:51

2 answers

2

From the posting in your question you know the duplicates then to check how often each value you can use #frequency() of Collections .

You said in the comments that you need to store the number of IDs for each ID. One way to do this is by using a map in which the key would be the id and the value would be the quantity.

An example, starting a list with the IDs you have would be this:

final List<Integer> ids = Arrays.asList(2, 1, 2, 2, 2, 2, 2, 1, 3, 3, 3, 3, 4, 5, 5, 6, 6, 6);

final Map<Integer, Integer> idQuantidade = new HashMap<>();

idQuantidade.put(1, Collections.frequency(ids, 1));
idQuantidade.put(2, Collections.frequency(ids, 2));
idQuantidade.put(6, Collections.frequency(ids, 6));

So, to print each value we would have something like this:

final String format = "ID: %d possui: %d calculos";
final Set<Integer> chaves = idQuantidade.keySet(); // as chaves são os ids
for (final Integer chave : chaves) {
    System.out.println(String.format(format, chave, idQuantidade.get(chave)));
}

It will display the following:

ID: 1 possui: 2 calculos
ID: 2 possui: 6 calculos
ID: 6 possui: 3 calculos

Another way, in Java 8+, would be to use the #compute() " of Map .

final Map<Integer, Integer> idQuantidade = new HashMap<>();

ids.forEach(id -> idQuantidade.compute(id, (chave, valor) -> (valor == null ? 1 : valor + 1)));

In this way, we iterate the values of the list and we fill the map, using the id as key and the value equal to 1 when there is no key - > value in maps or incrementing in 1 whenever an id already exists as a key. The impression of each value would look something like this:

idQuantidade.forEach((chave, valor) -> System.out.println(String.format("ID: %d possui: %d calculos", chave, idQuantidade.get(chave))));

It will display the following:

ID: 1 possui: 2 calculos
ID: 2 possui: 6 calculos
ID: 3 possui: 4 calculos
ID: 4 possui: 1 calculos
ID: 5 possui: 2 calculos
ID: 6 possui: 3 calculos

You can also have a data structure of your own, depending on your context.

    
17.05.2015 / 01:16
0

Since you have the values in a DB, why not get that data already in the form you want?

Assuming your DB access class is db and the column name you want to know the number of occurrences is produtoId , the following code gets a Cursor with the sum of the occurrences of produtoId and writes a log for each:

Cursor cursor = db.rawQuery(
        "SELECT produtoId, count(*) FROM nomeDaSuaTabela GROUP BY produroId", null);
while (cursor.moveToNext()) {
    Log.d(TAG,"ID: " + cursor.getInt(0) + " possui: cursor.getInt(1) calculos");
}
cursor.close(); 
    
17.05.2015 / 17:29