aboutsummaryrefslogtreecommitdiff
path: root/challenge-074/shawn-wagner/java/ch1.java
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-17 20:14:34 +0100
committerGitHub <noreply@github.com>2020-08-17 20:14:34 +0100
commit298bef1c7ca6244d542229e3a2b868ee6201b14a (patch)
treebb4327f7d670e7c86daa1445217a8a6ad3ea6a3c /challenge-074/shawn-wagner/java/ch1.java
parentbe7ef08333f2a20a1c2589ca69d57b542ba671f0 (diff)
parent218420ca9c9c744ce225312a44fe03110ff725b7 (diff)
downloadperlweeklychallenge-club-298bef1c7ca6244d542229e3a2b868ee6201b14a.tar.gz
perlweeklychallenge-club-298bef1c7ca6244d542229e3a2b868ee6201b14a.tar.bz2
perlweeklychallenge-club-298bef1c7ca6244d542229e3a2b868ee6201b14a.zip
Merge pull request #2093 from shawnw/challenge-074-solution
Solutions for challenge 074.
Diffstat (limited to 'challenge-074/shawn-wagner/java/ch1.java')
-rw-r--r--challenge-074/shawn-wagner/java/ch1.java26
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-074/shawn-wagner/java/ch1.java b/challenge-074/shawn-wagner/java/ch1.java
new file mode 100644
index 0000000000..7d14236d22
--- /dev/null
+++ b/challenge-074/shawn-wagner/java/ch1.java
@@ -0,0 +1,26 @@
+// Use streams to calculate the count of distinct elements in an array and
+// filter out just the first one above the cutoff.
+
+// I'm particularly proud of this one.
+
+import java.util.*;
+import java.util.stream.*;
+import java.util.function.Function;
+
+class ch1 {
+ private static void task1(int[] a) {
+ int cutoff = (int)Math.floor(a.length / 2.0);
+ int over_cutoff =
+ Arrays.stream(a).boxed()
+ .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
+ .entrySet().stream()
+ .filter(entry -> entry.getValue() > cutoff)
+ .findFirst().map(entry -> entry.getKey()).orElse(-1);
+ System.out.println(over_cutoff);
+ }
+
+ public static void main(String[] args) {
+ task1(new int[] {1, 2, 2, 3, 2, 4, 2});
+ task1(new int[] {1, 3, 1, 2, 4, 5});
+ }
+}