Everyone figured finding a duplicate in an array meant firing up a hash set — O(n) time, sure, but chowing down on extra space like it’s free buffet night. Or sort it, slog through O(n log n), pretend you’re efficient. Then bam: a Reddit post links a YouTube vid shouting find duplicate in an array in O(n). No space guzzlers. Constant memory. Changes the game? For certain games, yeah.
How to find duplicate in an array in O(n)
That’s the whole pitch. Short. Punchy. Clickable. But here’s the thing — it ain’t for your grandma’s grocery list of random strings. Strict rules apply.
The Setup Everyone Ignores
Picture this: array of n+1 integers. Each between 1 and n inclusive. Pigeonhole principle screams duplicate exists. Can’t modify the array. O(n) time. O(1) space. LeetCode 287, baby. That’s the arena.
Devs expected the usual suspects. Hash map: dump numbers, check collisions. Boom, average O(n), worst case nightmare if hashing flops. Sorting: rearrange, scan neighbors. Clean, but logarithmic drag. This method? Treats the array as a functional graph. Index by value. Chase pointers. Spot the cycle. Duplicate’s the loop entry.
Brilliant. Or is it?
Slow tortoise. Frantic hare. They meet in the cycle. Reset tortoise to start. Both crawl one step. Rendezvous at the duplicate. Floyd’s algorithm from 1967, dusted off for arrays. Historical parallel no one mentions: this exact trick cracked linked list cycles decades ago. Why’d we forget?
Step-by-Step: No Smoke, Just Mirrors
Grab nums = [1,3,4,2,2]. n=5, values 1-4? Wait, n+1=5, 1-4. Duplicate 2.
Tortoise at nums[0]=1, then nums[1]=3, nums[3]=2, nums[2]=4, nums[4]=2.
Hare zips ahead: nums[0]=1, nums[1]=3, nums[3]=2, nums[2]=4, nums[4]=2, nums[2]=4,…
They collide at second 2. Reset tortoise to 0 (1). Both step: tortoise to 3, hare to 4 then 2. Meet at 2. Duplicate found.
Code’s trivial in Python — two pointers, while loops. No extras. But try it on [5,3,4,2,1]. No duplicate? Crashes assumptions.
That’s the rub. Works like a charm under constraints. Elsewhere? Gibberish.
And.
It’s slow in practice sometimes — cache misses from jumping indices. But hey, Big O don’t care about constants.
Why Devs Are Losing It Anyway
Interviews. That’s why. FAANG loves this. Tests graph thinking, not just hash worship. Predicted it years ago: cycle detection variants everywhere now. TikTok to Threads, viral. Reddit comments explode: ‘Mind blown!’ ‘Genius!’
Corporate hype? Nah, solo Redditor. But same sin — title screams general solution. Clickbait much? O(n) time yes, but screams find duplicate in an array in O(n) like it’s universal. Read the fine print, folks.
Real world? Arrays of UUIDs? Hashes win. Massive logs? Sort or sample. This? Niche king.
Dry humor time: it’s the algorithm equivalent of that one uncle who fixes your car with duct tape. Works. Impressive. Don’t bet the farm.
Is This Better Than Hashing for Real Apps?
Short answer: no. Long answer — depends.
Hashing: flexible. Any hashables. Parallelizable. This? Integers only, pigeonholed range. Space edge in memory-strapped embedded? Sure. IoT sensors spitting 1-n+1 readings? Gold.
But 99% cases? Hash it. Or, wild idea, use a bitset if bits fit — still O(n) space-ish.
Critique the spin: YouTube thumbnails glow ‘O(n) miracle!’ Skip the ‘given n+1 elems 1-n’ caveat till minute 3. Classic.
Unique twist — pair it with XOR tricks for single number missing/dupe variants. Interview combo platter.
What If We Broke the Rules?
Drop constraints. General unsorted array, find any dupe.
Hash set reigns. Or Mo’s algorithm for offline queries — overkill.
Randomized: pick elements, hash pairs till hit. Expected O(sqrt(n)) probes, but meh.
This Floyd hack? Adapts poorly. Multiple dupes? Chaos. Floats? No indices.
So, worship wisely.
Look, it’s a gem. Teaches graphs hide everywhere. Pushes O(1) space thinking. But don’t tattoo it on your knuckles.
The Verdict: Hype Meets Reality
Changes things? For algo prep, yes. Production code? Meh.
Bold call: by 2025, every coding bootcamp drills this. LeetCode premiums spike.
Reddit dude deserves props. Vid’s clear — watch it. But next time, title it honest.
🧬 Related Insights
- Read more: DBmaestro’s MCP Server: AI Agents Finally Tamed for Database Hell
- Read more: The Dead Patreon Post That Woke Up My Audience — And the Brain Hack Behind It
Frequently Asked Questions
What is the O(n) way to find duplicate in array?
Uses Floyd’s cycle detection on array-as-graph. Assumes n+1 elements valued 1 to n. Tortoise-hare pointers find cycle entry: the dupe.
Does this work on any array?
Nope. Strict constraints: exactly one dupe, integers 1-n in n+1 spots. General case? Hash or sort.
Why O(1) space here but not hashing?
No extra structures — just indices. Hash needs n buckets worst-case.