Skip to content

Commit

Permalink
feat: Improvement of ArrayMode by adding ArrayModeInPlace implementat…
Browse files Browse the repository at this point in the history
…ion (#211)

* Create ArrayModeInPlaceSnippet.java and ArrayModeInPlaceSnippetTest.java

* Fix ArrayModeInPlaceSnippetTest

* Fix the ArrayModeInPlaceSnippetTest line length issue

* update README
  • Loading branch information
XieGuochao authored May 25, 2024
1 parent 1494916 commit d7b11b6
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/array/ArrayModeInPlaceSnippet.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
45 changes: 45 additions & 0 deletions src/test/java/array/ArrayModeInPlaceSnippetTest.java
Original file line number Diff line number Diff line change
@@ -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 ArrayModeInPlaceSnippet #ArrayModeInPlaceSnippet(int[])}.
*/
@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, -6}));
assertEquals(0, ArrayModeInPlaceSnippet.modeArrayInPlace(new int[]{-4, 0, -2, -1, 0}));
assertEquals(1, ArrayModeInPlaceSnippet.modeArrayInPlace(new int[]{1, 1, 1, 1, 1, 1}));
}
}

0 comments on commit d7b11b6

Please sign in to comment.