Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Example 1:
Input: nums = [3,2,3] Output: 3
Example 2:
Input: nums = [2,2,1,1,1,2,2] Output: 2
Constraints:
n == nums.length1 <= n <= 5 * 104-109 <= nums[i] <= 109O(1) space?Problem summary: Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[3,2,3]
[2,2,1,1,1,2,2]
majority-element-ii)check-if-a-number-is-majority-element-in-a-sorted-array)most-frequent-even-element)minimum-index-of-a-valid-split)minimum-operations-to-exceed-threshold-value-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #169: Majority Element
class Solution {
public int majorityElement(int[] nums) {
int cnt = 0, m = 0;
for (int x : nums) {
if (cnt == 0) {
m = x;
cnt = 1;
} else {
cnt += m == x ? 1 : -1;
}
}
return m;
}
}
// Accepted solution for LeetCode #169: Majority Element
func majorityElement(nums []int) int {
var cnt, m int
for _, x := range nums {
if cnt == 0 {
m, cnt = x, 1
} else {
if m == x {
cnt++
} else {
cnt--
}
}
}
return m
}
# Accepted solution for LeetCode #169: Majority Element
class Solution:
def majorityElement(self, nums: List[int]) -> int:
cnt = m = 0
for x in nums:
if cnt == 0:
m, cnt = x, 1
else:
cnt += 1 if m == x else -1
return m
// Accepted solution for LeetCode #169: Majority Element
impl Solution {
pub fn majority_element(nums: Vec<i32>) -> i32 {
let mut m = 0;
let mut cnt = 0;
for &x in nums.iter() {
if cnt == 0 {
m = x;
cnt = 1;
} else {
cnt += if m == x { 1 } else { -1 };
}
}
m
}
}
// Accepted solution for LeetCode #169: Majority Element
function majorityElement(nums: number[]): number {
let cnt: number = 0;
let m: number = 0;
for (const x of nums) {
if (cnt === 0) {
m = x;
cnt = 1;
} else {
cnt += m === x ? 1 : -1;
}
}
return m;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.