diff options
Diffstat (limited to 'challenge-276/deadmarshal/java/Ch2.java')
| -rw-r--r-- | challenge-276/deadmarshal/java/Ch2.java | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/challenge-276/deadmarshal/java/Ch2.java b/challenge-276/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..c8237e9f2b --- /dev/null +++ b/challenge-276/deadmarshal/java/Ch2.java @@ -0,0 +1,18 @@ +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class Ch2 { + public static void main(String[] args) { + System.out.println(maximum_frequency(new int[]{1, 2, 2, 4, 1, 5})); + System.out.println(maximum_frequency(new int[]{1, 2, 3, 4, 5})); + } + + private static int maximum_frequency(int[] arr) { + Map<Integer, Integer> hash = new HashMap<>(); + for (int e : arr) hash.put(e, hash.getOrDefault(e, 0) + 1); + int max = Collections.max(hash.values()); + return (int) hash.keySet().stream() + .filter(e -> hash.get(e) == max).count() * max; + } +} |
