The idea is that from any list Object
in java, the mode of the elements can be obtained. This means ALL the elements that have the same and maximum frequency.
The example is the following list:
ArrayList<Object> strings = new ArrayList<>(
Arrays.asList("hello", "world", "my",
"world", "is", "you", "and",
"your", "world", "is", "me",
"is", "love", "this", "?")
);
Where we see that the words are repeated 3 times: " hello " and " is "
The result should be something similar to:
Object | frequency |
---|---|
"Hello" | 3 |
"es" | 3 |
Observations
String.equalsIgnoreCase
orInteger.compare
, if not, we must use methods of the Object class , that is, we will have to limit ourselves to the use of the Object.equals methodTo solve the problem
Solution 1
annotations
Solution 2
annotations
Contrary to the first solution, here we first find the maximum frequency, doing the Stream.mapToInt of the frequencies and selecting the maximum. After that, those Objects whose frequency coincides with the maximum are added to the HashMap .
Personally, I prefer solution 2 because I think it's more efficient (at first glance, because you don't have to filter and re-create a map), although both solutions seem totally valid to me