From cccc898ba1577bcb94168c2218b771add743b4c0 Mon Sep 17 00:00:00 2001 From: gijsh21 <69896881+gijsh21@users.noreply.github.com> Date: Thu, 20 Aug 2020 11:18:36 +0200 Subject: [PATCH 1/2] Create Mode.java For finding the mode of an array of numbers --- Maths/Mode.java | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Maths/Mode.java diff --git a/Maths/Mode.java b/Maths/Mode.java new file mode 100644 index 00000000..e2b7f0d9 --- /dev/null +++ b/Maths/Mode.java @@ -0,0 +1,69 @@ +package Maths; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; + +/* + * Find the mode of an array of numbers + * + * The mode of an array of numbers is the most frequently occurring number in the array, + * or the most frequently occurring numbers if there are multiple numbers with the same frequency + */ +public class Mode { + + public static void main(String[] args) { + + /* Test array of integers */ + assert (mode(new int[]{})) == null; + assert Arrays.equals(mode(new int[]{5}), new int[]{5}); + assert Arrays.equals(mode(new int[]{7, 9, 9, 4, 5, 6, 7, 7, 8}), new int[]{7}); + assert Arrays.equals(mode(new int[]{7, 9, 9, 4, 5, 6, 7, 7, 9}), new int[]{7, 9}); + + } + + /* + * Find the mode of an array of integers + * + * @param int[] array of integers + * @return int[] mode of the array + */ + public static int[] mode(int[] numbers) { + + if(numbers.length == 0) return null; + + HashMap count = new HashMap(); + + for(int num : numbers) { + + if(count.containsKey(num)) { + + count.put(num, count.get(num) + 1); + + } else { + + count.put(num, 1); + + } + + } + + int max = Collections.max(count.values()); + ArrayList modes = new ArrayList(); + + for(int num : count.keySet()) { + + if(count.get(num) == max) { + + modes.add(num); + + } + + } + + return modes.stream().mapToInt(n -> n).toArray(); + + } + +} From bbe6f9410c9e734cc0426b1fa40f454fe9dc639d Mon Sep 17 00:00:00 2001 From: gijsh21 <69896881+gijsh21@users.noreply.github.com> Date: Thu, 20 Aug 2020 15:25:10 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Du Yuanchao --- Maths/Mode.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/Maths/Mode.java b/Maths/Mode.java index e2b7f0d9..d43ea16e 100644 --- a/Maths/Mode.java +++ b/Maths/Mode.java @@ -18,6 +18,7 @@ public class Mode { /* Test array of integers */ assert (mode(new int[]{})) == null; assert Arrays.equals(mode(new int[]{5}), new int[]{5}); + assert Arrays.equals(mode(new int[]{1, 2, 3, 4, 5}), new int[]{1, 2, 3, 4, 5}); assert Arrays.equals(mode(new int[]{7, 9, 9, 4, 5, 6, 7, 7, 8}), new int[]{7}); assert Arrays.equals(mode(new int[]{7, 9, 9, 4, 5, 6, 7, 7, 9}), new int[]{7, 9}); @@ -26,17 +27,16 @@ public class Mode { /* * Find the mode of an array of integers * - * @param int[] array of integers - * @return int[] mode of the array + * @param numbers array of integers + * @return mode of the array */ public static int[] mode(int[] numbers) { if(numbers.length == 0) return null; - HashMap count = new HashMap(); + HashMap count = new HashMap<>(); for(int num : numbers) { - if(count.containsKey(num)) { count.put(num, count.get(num) + 1); @@ -50,20 +50,14 @@ public class Mode { } int max = Collections.max(count.values()); - ArrayList modes = new ArrayList(); + ArrayList modes = new ArrayList<>(); for(int num : count.keySet()) { - if(count.get(num) == max) { - modes.add(num); - } - } - return modes.stream().mapToInt(n -> n).toArray(); - } }