Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java-get most common element in a list

Tags:

Does Java or Guava have something that will return most common element in a list?

List<BigDecimal> listOfNumbers=  new ArrayList<BigDecimal>();  

[1,3,4,3,4,3,2,3,3,3,3,3]

return 3

like image 586
Doc Holiday Avatar asked Sep 26 '13 14:09

Doc Holiday


People also ask

How do you find the most common element in a list Python?

Make use of Python Counter which returns count of each element in the list. Thus, we simply find the most common element by using most_common() method.


2 Answers

In statistics, this is called the "mode". A vanilla Java 8 solution looks like this:

Stream.of(1, 3, 4, 3, 4, 3, 2, 3, 3, 3, 3, 3)       .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))       .entrySet()       .stream()       .max(Map.Entry.comparingByValue())       .ifPresent(System.out::println); 

Which yields:

3=8 

jOOλ is a library that supports mode() on streams. The following program:

System.out.println(     Seq.of(1, 3, 4, 3, 4, 3, 2, 3, 3, 3, 3, 3)        .mode() ); 

Yields:

Optional[3] 

For simplicity's sake, I omitted using BigDecimal. The solution would be the same, though.

(disclaimer: I work for the company behind jOOλ)

like image 92
Lukas Eder Avatar answered Oct 07 '22 17:10

Lukas Eder


This is fairly easy to implement yourself:

public static <T> T mostCommon(List<T> list) {     Map<T, Integer> map = new HashMap<>();      for (T t : list) {         Integer val = map.get(t);         map.put(t, val == null ? 1 : val + 1);     }      Entry<T, Integer> max = null;      for (Entry<T, Integer> e : map.entrySet()) {         if (max == null || e.getValue() > max.getValue())             max = e;     }      return max.getKey(); } 

List<Integer> list = Arrays.asList(1,3,4,3,4,3,2,3,3,3,3,3); System.out.println(mostCommon(list)); 
 3 

If you want to handle cases where there's more then one most frequent element, you can scan the list once to determine how many times the most frequent element(s) occur, and then scan the list again, put those elements in a set and return that.

like image 45
arshajii Avatar answered Oct 07 '22 16:10

arshajii