Skip to content

Commit

Permalink
feat: Improvement to mode algorithm #206 (#238)
Browse files Browse the repository at this point in the history
* fix: Improvement to mode algorithm #206

* Fixed checkstyle errors

* Fixed checkstyle errors

* Added Code to Read Me file
  • Loading branch information
sharara6 authored Jan 11, 2025
1 parent 212b7c4 commit b444a72
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 51 deletions.
45 changes: 23 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> modeArray(int[] arr) {
int maxCount = 0;
HashMap<Integer, Integer> 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<Integer> modes = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
if (entry.getValue() == maxCount) {
modes.add(entry.getKey());
}
}
return modes;
}
return mode;
}
}
```

Expand Down
46 changes: 26 additions & 20 deletions src/main/java/array/ArrayModeSnippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> modeArray(int[] arr) {
int maxCount = 0;
HashMap<Integer, Integer> frequencyMap = new HashMap<>();
for (int num : arr) {
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
maxCount = Math.max(maxCount, frequencyMap.get(num));
}
List<Integer> modes = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
if (entry.getValue() == maxCount) {
modes.add(entry.getKey());
}
}
return mode;
return modes;
}
}
20 changes: 11 additions & 9 deletions src/test/java/array/ArrayModeSnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}));
}
}
}

0 comments on commit b444a72

Please sign in to comment.