Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count duplicate elements in ArrayList?

I need to separate and count how many values in arraylist are the same and print them according to the number of occurrences.

I've got an arraylist called digits :

 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]

I created a method which separates each value and saves it to a new array.

public static ArrayList<Integer> myNumbers(int z) {

    ArrayList<Integer> digits = new ArrayList<Integer>();
    String number = String.valueOf(z);
    for (int a = 0; a < number.length(); a++) {
        int j = Character.digit(number.charAt(a), 10);
        digits.add(j);
    }
    return digits;

}

After this I've got a new array called numbers. I'm using sort on this array

Collections.sort(numbers);

and my ArrayList looks like this:

[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9]

It has:

2 times 0; 
9 times 1;
4 times 2;
6 times 3;
5 times 4;
6 times 5;
5 times 6;
5 times 7;
5 times 8;
3 times 9;

I need to print out the string of numbers depend on how many are they So it suppose to look like this : 1354678290

like image 258
Piotr Szczepanik Avatar asked Jun 05 '17 10:06

Piotr Szczepanik


People also ask

How do you count duplicate elements in a list?

If you want to count duplicates for a given element then use the count() function. Use a counter() function or basics logic combination to find all duplicated elements in a list and count them in Python.

How do you count elements in an ArrayList?

The size of an ArrayList can be obtained by using the java. util. ArrayList. size() method as it returns the number of elements in the ArrayList i.e. the size.


2 Answers

List<String> list = new ArrayList<String>();
    list.add("a");
    list.add("b");
    list.add("c");
    list.add("a");
    list.add("a");
    list.add("a");

int countA=Collections.frequency(list, "a");
int countB=Collections.frequency(list, "b");
int countC=Collections.frequency(list, "c");
like image 131
pruthwiraj.kadam Avatar answered Oct 03 '22 07:10

pruthwiraj.kadam


The question is to count how many ones twos and threes are there in an array. In Java 7 solution is:

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class howMany1 {
public static void main(String[] args) {

    List<Integer> list = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765);

    Map<Integer ,Integer> map = new HashMap<>();

      for(  Integer r  : list) {
          if(  map.containsKey(r)   ) {
                 map.put(r, map.get(r) + 1);
          }//if
          else {
              map.put(r, 1);
          }
      }//for

      //iterate

      Set< Map.Entry<Integer ,Integer> > entrySet = map.entrySet();
      for(    Map.Entry<Integer ,Integer>  entry : entrySet     ) {
          System.out.printf(   "%s : %d %n "    , entry.getKey(),entry.getValue()  );
      }//for

}}

In Java 8, the solution to the problem is :

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class howMany2 {
public static void main(String[] args) {

    List<Integer> list = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765);
     // we can also use Function.identity() instead of c->c
    Map<Integer ,Long > map = list.stream()
            .collect(  Collectors.groupingBy(c ->c , Collectors.counting())         ) ;


    map.forEach(   (k , v ) -> System.out.println( k + " : "+ v )                    );

}}

One another method is to use Collections.frequency. The solution is:

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Duplicates1 {
public static void main(String[] args) {

    List<Integer> list = Arrays.asList(1, 1, 2, 3, 5, 8, 13,13, 21, 34, 55, 89, 144, 233);

    System.out.println("Count all with frequency");
    Set<Integer> set = new HashSet<Integer>(list);
    for (Integer r : set) {
        System.out.println(r + ": " + Collections.frequency(list, r));
    }

}}

Another method is to change the int array to Integer List using method => Arrays.stream(array).boxed().collect(Collectors.toList()) and then get the integer using for loop.

public class t7 {
    public static void main(String[] args) {
        int[] a = { 1, 1, 2, 3, 5, 8, 13, 13 };
        List<Integer> list = Arrays.stream(a).boxed().collect(Collectors.toList());

        for (Integer ch : list) {
            System.out.println(ch + " :  " + Collections.frequency(list, ch));
        }

    }// main
}
like image 26
Soudipta Dutta Avatar answered Oct 03 '22 06:10

Soudipta Dutta