Skip to content

refactor: Mode #6381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/main/java/com/thealgorithms/maths/Mode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,49 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* 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
/**
* Utility class to calculate the mode(s) of an array of integers.
* <p>
* The mode of an array is the integer value(s) that occur most frequently.
* If multiple values have the same highest frequency, all such values are returned.
* </p>
*/
public final class Mode {
private Mode() {
}

/*
* Find the mode of an array of integers
/**
* Computes the mode(s) of the specified array of integers.
* <p>
* If the input array is empty, this method returns {@code null}.
* If multiple numbers share the highest frequency, all are returned in the result array.
* </p>
*
* @param numbers array of integers
* @return mode of the array
* @param numbers an array of integers to analyze
* @return an array containing the mode(s) of the input array, or {@code null} if the input is empty
*/
public static int[] mode(final int[] numbers) {
if (numbers.length == 0) {
return null;
}

HashMap<Integer, Integer> count = new HashMap<>();
Map<Integer, Integer> count = new HashMap<>();

for (int num : numbers) {
if (count.containsKey(num)) {
count.put(num, count.get(num) + 1);
} else {
count.put(num, 1);
}
count.put(num, count.getOrDefault(num, 0) + 1);
}

int max = Collections.max(count.values());
ArrayList<Integer> modes = new ArrayList<>();
List<Integer> modes = new ArrayList<>();

for (final var entry : count.entrySet()) {
if (entry.getValue() == max) {
modes.add(entry.getKey());
}
}
return modes.stream().mapToInt(n -> n).toArray();
return modes.stream().mapToInt(Integer::intValue).toArray();
}
}
Loading