Is there a way to find the most common String in an ArrayList?
ArrayList list = new ArrayList<>();
list.add(\"t
You can use Guava's Multiset:
ArrayList names = ...
// count names
HashMultiset namesCounts = HashMultiset.create(names);
Set> namesAndCounts = namesCounts.entrySet();
// find one most common
Multiset.Entry maxNameByCount = Collections.max(namesAndCounts, Comparator.comparing(Multiset.Entry::getCount));
// pick all with the same number of occurrences
List mostCommonNames = new ArrayList<>();
for (Multiset.Entry nameAndCount : namesAndCounts) {
if (nameAndCount.getCount() == maxNameByCount.getCount()) {
mostCommonNames.add(nameAndCount.getElement());
}
}