Java Array Program · A24
Find the Majority Element in an Array in Java
Pair each occurrence of the majority value with a different value and cancel the pair. Because the majority has more than half of the elements, it is the one left standing.
Problem Statement
Return the element that appears more than n / 2 times in an array of length n, if such an element exists.
If no element has a strict majority, return an empty result (printed as none).
Input/Output Examples
[3, 3, 4, 3, 2, 3, 3]Output3Explanation: 3 appears 5 times out of 7.[1, 2, 3]OutputnoneExplanation: every value appears once.[7]Output7Explanation: 1 is more than half of 1.Visual Explanation
When the votes drop to zero, the next value becomes the new candidate. A true majority can never be cancelled out completely, but the second pass is needed to confirm that a majority exists at all.
Approaches
The voting algorithm uses constant memory. The map approach counts every value.
Approach 1: Boyer-Moore Voting (Recommended)
Idea: keep a candidate and a vote count; equal values add a vote, different values remove one, and zero votes means a new candidate.
- If
votes == 0, make the current value the candidate. - Add 1 if the value equals the candidate, otherwise subtract 1.
- In a second pass, count the candidate and return it only if it appears more than
n / 2times.
Time: O(n) · Space: O(1).
Approach 2: HashMap Counts
Idea: count each value and return as soon as one count exceeds n / 2.
- Use
merge(n, 1, Integer::sum)for each value. - If the new count is above
n / 2, return the value. - Return empty if the loop ends.
Time: O(n) · Space: O(k).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Boyer-Moore Voting (Recommended)
The verification pass is essential when a majority is not guaranteed; without it, [1, 2, 3] would return 3.
import java.util.Arrays;
import java.util.OptionalInt;
public class MajorityVoting {
// Boyer-Moore: pairs of different values cancel; a true majority always survives
static OptionalInt majority(int[] arr) {
int candidate = 0;
int votes = 0;
for (int n : arr) {
if (votes == 0) {
candidate = n;
}
votes += (n == candidate) ? 1 : -1;
}
int count = 0; // second pass: confirm the candidate
for (int n : arr) {
if (n == candidate) {
count++;
}
}
return count > arr.length / 2 ? OptionalInt.of(candidate) : OptionalInt.empty();
}
public static void main(String[] args) {
int[][] inputs = {{3, 3, 4, 3, 2, 3, 3}, {1, 2, 3}, {7}};
for (int[] arr : inputs) {
OptionalInt m = majority(arr);
System.out.println(Arrays.toString(arr) + " -> " + (m.isPresent() ? m.getAsInt() : "none"));
}
}
}
[3, 3, 4, 3, 2, 3, 3] -> 3 [1, 2, 3] -> none [7] -> 7
Time complexity: O(n) · Space complexity: O(1).
Approach 2: HashMap Counts
Straightforward, and it can stop early, but it needs memory for every distinct value.
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.OptionalInt;
public class MajorityMap {
// Counts every value and returns one whose count exceeds n / 2
static OptionalInt majority(int[] arr) {
Map<Integer, Integer> counts = new HashMap<>();
for (int n : arr) {
if (counts.merge(n, 1, Integer::sum) > arr.length / 2) {
return OptionalInt.of(n);
}
}
return OptionalInt.empty();
}
public static void main(String[] args) {
int[][] inputs = {{3, 3, 4, 3, 2, 3, 3}, {1, 2, 3}, {7}};
for (int[] arr : inputs) {
OptionalInt m = majority(arr);
System.out.println(Arrays.toString(arr) + " -> " + (m.isPresent() ? m.getAsInt() : "none"));
}
}
}
[3, 3, 4, 3, 2, 3, 3] -> 3 [1, 2, 3] -> none [7] -> 7
Time complexity: O(n) · Space complexity: O(k).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Boyer-Moore Voting | O(n) | O(1) |
| HashMap Counts | O(n) | O(k) |
Here n is the number of elements and k the number of distinct values. Voting needs two passes and two variables; counting needs one pass and a map.
Interview Tips and Recommended Approach
- Always verify unless a majority is guaranteed. The candidate is meaningless otherwise.
- Explain the cancellation idea rather than just reciting the code.
- Majority means more than half. Exactly half, as in
[1, 1, 2, 2], is not a majority. - Know the extension. Elements appearing more than
n / 3times need two candidates.
Recommended Approach: Boyer-Moore Voting
Why: it finds the majority in linear time with only two variables, which is the elegant answer interviewers hope to hear.