Skip to content
ArrayMedium

Elements Appearing > n/3 Times

Extended Boyer-Moore maintains two candidates to find elements appearing > n/3 times.

Time O(n) Space O(1)
Open in 3D Studio

Live loop — open the studio to scrub, add your own array, or race it in Battle Arena.

Asked in interviews at

UberMicrosoftDoorDash

Reference implementation (Python)

def majority_element_n3(arr):
    c1 = c2 = None; cnt1 = cnt2 = 0
    for x in arr:
        if x == c1: cnt1 += 1
        elif x == c2: cnt2 += 1
        elif cnt1 == 0: c1, cnt1 = x, 1
        elif cnt2 == 0: c2, cnt2 = x, 1
        else: cnt1 -= 1; cnt2 -= 1
    return [x for x in (c1, c2) if arr.count(x) > len(arr)//3]

Embed this visualization

Drop a live, looping Elements Appearing > n/3 Times canvas into a blog post or course page.

<iframe src="/embed/elements-more-than-n-third" width="100%" height="380" style="border:1px solid #e6e8ec;border-radius:14px" loading="lazy" title="3D algorithm visualization by CodeWithMunnaX"></iframe>

Frequently asked

What is the time and space complexity of Elements Appearing > n/3 Times?

Elements Appearing > n/3 Times runs in O(n) time and O(1) auxiliary space.

Is the Elements Appearing > n/3 Times visualizer free?

The core 3D visualizer, Web Audio chimes and step intuition are free. A one-time ₹49 Pro Pass unlocks advanced algorithms, large custom arrays, Battle Arena and reel export.

Which companies ask Elements Appearing > n/3 Times in interviews?

Elements Appearing > n/3 Times shows up in interviews at Uber, Microsoft, DoorDash.

How does the 3D visualization of Elements Appearing > n/3 Times work?

Each array value becomes a 3D pillar whose height maps to its value. Comparisons glow amber, swaps flash red, and settled elements lock green while a floating pointer badge tracks the active index — synchronized line-by-line with the Python code.

More Array