From b444a72996fd10a1d94b9a243dd4fce56deb52f5 Mon Sep 17 00:00:00 2001 From: Faris Sharara Date: Sat, 11 Jan 2025 19:29:35 +0200 Subject: [PATCH] feat: Improvement to mode algorithm #206 (#238) * fix: Improvement to mode algorithm #206 * Fixed checkstyle errors * Fixed checkstyle errors * Added Code to Read Me file --- README.md | 45 +++++++++--------- src/main/java/array/ArrayModeSnippet.java | 46 +++++++++++-------- src/test/java/array/ArrayModeSnippetTest.java | 20 ++++---- 3 files changed, 60 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index cc63f296..8444cadc 100644 --- a/README.md +++ b/README.md @@ -720,31 +720,32 @@ public class ArrayModeInPlaceSnippet { ```java public class ArrayModeSnippet { - /** - * Returns the mode of the array. - * - * @param arr array to find mode in it - * @return mode of array - */ - public static int modeArray(int[] arr) { - int mode = 0; - int maxcount = 0; - - for (int i = 0; i < arr.length; i++) { - int count = 0; + /** + * Private constructor to prevent instantiation. + */ + private ArrayModeSnippet() { + throw new IllegalStateException("Utility class"); + } - for (int j = 0; j < arr.length; j++) { - if (arr[i] == arr[j]) { - count++; + /** + * Returns the mode(s) of the array. + * If multiple modes exist, it returns them in a list. + */ + public static List modeArray(int[] arr) { + int maxCount = 0; + HashMap frequencyMap = new HashMap<>(); + for (int num : arr) { + frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); + maxCount = Math.max(maxCount, frequencyMap.get(num)); } - } - if (count > maxcount) { - maxcount = count; - mode = arr[i]; - } + List modes = new ArrayList<>(); + for (Map.Entry entry : frequencyMap.entrySet()) { + if (entry.getValue() == maxCount) { + modes.add(entry.getKey()); + } + } + return modes; } - return mode; - } } ``` diff --git a/src/main/java/array/ArrayModeSnippet.java b/src/main/java/array/ArrayModeSnippet.java index 4b7a11e2..bc56d80f 100644 --- a/src/main/java/array/ArrayModeSnippet.java +++ b/src/main/java/array/ArrayModeSnippet.java @@ -24,34 +24,40 @@ package array; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * ArrayModeSnippet. */ + public class ArrayModeSnippet { /** - * Returns the mode of the array. - * - * @param arr array to find mode in it - * @return mode of array - */ - public static int modeArray(int[] arr) { - int mode = 0; - int maxcount = 0; - - for (int i = 0; i < arr.length; i++) { - int count = 0; + * Private constructor to prevent instantiation. + */ + private ArrayModeSnippet() { + throw new IllegalStateException("Utility class"); + } - for (int j = 0; j < arr.length; j++) { - if (arr[i] == arr[j]) { - count++; - } - } - if (count > maxcount) { - maxcount = count; - mode = arr[i]; + /** + * Returns the mode(s) of the array. + * If multiple modes exist, it returns them in a list. + */ + public static List modeArray(int[] arr) { + int maxCount = 0; + HashMap frequencyMap = new HashMap<>(); + for (int num : arr) { + frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); + maxCount = Math.max(maxCount, frequencyMap.get(num)); + } + List modes = new ArrayList<>(); + for (Map.Entry entry : frequencyMap.entrySet()) { + if (entry.getValue() == maxCount) { + modes.add(entry.getKey()); } } - return mode; + return modes; } } diff --git a/src/test/java/array/ArrayModeSnippetTest.java b/src/test/java/array/ArrayModeSnippetTest.java index 1b3b80ee..47c29b70 100644 --- a/src/test/java/array/ArrayModeSnippetTest.java +++ b/src/test/java/array/ArrayModeSnippetTest.java @@ -26,20 +26,22 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.List; import org.junit.jupiter.api.Test; /** * Tests for 30 Seconds of Java code library. */ -public class ArrayModeSnippetTest { +class ArrayModeSnippetTest { /** - * Test for {@link ArrayModeSnippet #ArrayModeSnippet(int[])}. - */ + * Test for {@link ArrayModeSnippet #ArrayModeSnippet(int[])}. + */ @Test - void testModeArray() { - assertEquals(2, ArrayModeSnippet.modeArray(new int[]{1, 2, 3, 2, 4, 2, 2})); - assertEquals(-8, ArrayModeSnippet.modeArray(new int[]{-43, -8, -8, -10, -8, -65, -9})); - assertEquals(0, ArrayModeSnippet.modeArray(new int[]{-4, 0, -2, -1, 0})); - assertEquals(1, ArrayModeSnippet.modeArray(new int[]{1, 1, 1, 1, 1, 1})); + void testModeArray() { + assertEquals(List.of(2), ArrayModeSnippet.modeArray(new int[]{1, 2, 2, 3})); + assertEquals(List.of(2, 3), ArrayModeSnippet.modeArray(new int[]{1, 2, 2, 3, 3})); + assertEquals(List.of(1, 2, 3, 4), ArrayModeSnippet.modeArray(new int[]{1, 2, 3, 4})); + assertEquals(List.of(), ArrayModeSnippet.modeArray(new int[]{})); + assertEquals(List.of(-1, -2), ArrayModeSnippet.modeArray(new int[]{-1, -1, -2, -2, -3})); } -} +} \ No newline at end of file