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
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.
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λ)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With