From bdd2ac74d8f287d67d715427726d1d9fbbc46203 Mon Sep 17 00:00:00 2001 From: XieGuochao <117010307@link.cuhk.edu.cn> Date: Thu, 23 May 2024 17:10:21 +0200 Subject: [PATCH 1/4] Create ArrayModeInPlaceSnippet.java and ArrayModeInPlaceSnippetTest.java --- .../java/array/ArrayModeInPlaceSnippet.java | 67 +++++++++++++++++++ .../array/ArrayModeInPlaceSnippetTest.java | 45 +++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/main/java/array/ArrayModeInPlaceSnippet.java create mode 100644 src/test/java/array/ArrayModeInPlaceSnippetTest.java diff --git a/src/main/java/array/ArrayModeInPlaceSnippet.java b/src/main/java/array/ArrayModeInPlaceSnippet.java new file mode 100644 index 00000000..6dd80aeb --- /dev/null +++ b/src/main/java/array/ArrayModeInPlaceSnippet.java @@ -0,0 +1,67 @@ +/* + * MIT License + * + * Copyright (c) 2017-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package array; + +import java.util.Arrays; + +/** + * ArrayModeSnippet. + */ +public class ArrayModeInPlaceSnippet { + + /** + * Returns the mode of the array. + * + * @param arr array to find mode in it + * @return mode of array + */ + public static int modeArrayInPlace(int[] arr) { + if (arr.length == 0) { + return 0; + } + + Arrays.sort(arr); + + int mode = arr[0]; + int maxcount = 1; + int count = 1; + + for (int i = 1; i < arr.length; i++) { + if (arr[i] == arr[i - 1]) { + count++; + } else { + if (count > maxcount) { + maxcount = count; + mode = arr[i - 1]; + } + count = 1; + } + } + if (count > maxcount) { + mode = arr[arr.length - 1]; + } + return mode; + } +} diff --git a/src/test/java/array/ArrayModeInPlaceSnippetTest.java b/src/test/java/array/ArrayModeInPlaceSnippetTest.java new file mode 100644 index 00000000..9b7cb908 --- /dev/null +++ b/src/test/java/array/ArrayModeInPlaceSnippetTest.java @@ -0,0 +1,45 @@ +/* + * MIT License + * + * Copyright (c) 2017-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package array; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/** + * Tests for 30 Seconds of Java code library. + */ +public class ArrayModeInPlaceSnippetTest { + /** + * 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})); + } +} From e19ad1f7e97c2e8c18c6ab7644c77019536a688f Mon Sep 17 00:00:00 2001 From: XieGuochao <117010307@link.cuhk.edu.cn> Date: Thu, 23 May 2024 17:13:42 +0200 Subject: [PATCH 2/4] Fix ArrayModeInPlaceSnippetTest --- src/test/java/array/ArrayModeInPlaceSnippetTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/array/ArrayModeInPlaceSnippetTest.java b/src/test/java/array/ArrayModeInPlaceSnippetTest.java index 9b7cb908..7f97ee9f 100644 --- a/src/test/java/array/ArrayModeInPlaceSnippetTest.java +++ b/src/test/java/array/ArrayModeInPlaceSnippetTest.java @@ -33,13 +33,13 @@ */ public class ArrayModeInPlaceSnippetTest { /** - * Test for {@link ArrayModeSnippet #ArrayModeSnippet(int[])}. + * Test for {@link ArrayModeInPlaceSnippet #ArrayModeInPlaceSnippet(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})); + assertEquals(2, ArrayModeInPlaceSnippet.modeArrayInPlace(new int[]{1, 2, 3, 2, 4, 2, 2})); + assertEquals(-8, ArrayModeInPlaceSnippet.modeArrayInPlace(new int[]{-43, -8, -8, -10, -8, -65, -9})); + assertEquals(0, ArrayModeInPlaceSnippet.modeArrayInPlace(new int[]{-4, 0, -2, -1, 0})); + assertEquals(1, ArrayModeInPlaceSnippet.modeArrayInPlace(new int[]{1, 1, 1, 1, 1, 1})); } } From c7d32a91b4b3bf513a7bb1c5fb42f29058ac5522 Mon Sep 17 00:00:00 2001 From: XieGuochao <117010307@link.cuhk.edu.cn> Date: Thu, 23 May 2024 17:19:22 +0200 Subject: [PATCH 3/4] Fix the ArrayModeInPlaceSnippetTest line length issue --- src/test/java/array/ArrayModeInPlaceSnippetTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/array/ArrayModeInPlaceSnippetTest.java b/src/test/java/array/ArrayModeInPlaceSnippetTest.java index 7f97ee9f..ea3f81ad 100644 --- a/src/test/java/array/ArrayModeInPlaceSnippetTest.java +++ b/src/test/java/array/ArrayModeInPlaceSnippetTest.java @@ -38,7 +38,7 @@ public class ArrayModeInPlaceSnippetTest { @Test void testModeArray() { assertEquals(2, ArrayModeInPlaceSnippet.modeArrayInPlace(new int[]{1, 2, 3, 2, 4, 2, 2})); - assertEquals(-8, ArrayModeInPlaceSnippet.modeArrayInPlace(new int[]{-43, -8, -8, -10, -8, -65, -9})); + assertEquals(-8, ArrayModeInPlaceSnippet.modeArrayInPlace(new int[]{-43, -8, -8, -10, -8, -6})); assertEquals(0, ArrayModeInPlaceSnippet.modeArrayInPlace(new int[]{-4, 0, -2, -1, 0})); assertEquals(1, ArrayModeInPlaceSnippet.modeArrayInPlace(new int[]{1, 1, 1, 1, 1, 1})); } From a8fed56744e7467c8584ba0302e5bddf324e08c8 Mon Sep 17 00:00:00 2001 From: XieGuochao <117010307@link.cuhk.edu.cn> Date: Fri, 24 May 2024 15:06:29 +0200 Subject: [PATCH 4/4] update README --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index c85d7d2d..03a56e15 100644 --- a/README.md +++ b/README.md @@ -401,6 +401,38 @@ public static int modeArray(int[] arr) { } ``` +### Find mode of integer array (2) + +```java + public static int modeArrayInPlace(int[] arr) { + if (arr.length == 0) { + return 0; + } + + Arrays.sort(arr); + + int mode = arr[0]; + int maxcount = 1; + int count = 1; + + for (int i = 1; i < arr.length; i++) { + if (arr[i] == arr[i - 1]) { + count++; + } else { + if (count > maxcount) { + maxcount = count; + mode = arr[i - 1]; + } + count = 1; + } + } + if (count > maxcount) { + mode = arr[arr.length - 1]; + } + return mode; + } +``` + ### Find sum of integer array ```java