Find Duplicate Number in Array
Cycle Detection (Floyd's Tortoise and Hare)Solved Oct 26, 2025
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven an array nums containing n + 1 integers where each integer is between 1 and n (inclusive). Assume there is only one duplicate number, find and return this duplicate. The solution must use O(1) extra space and run in less than O(n²) time.ReadHide
Problem Description
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive). Assume there is only one duplicate number, find and return this duplicate. The solution must use O(1) extra space and run in less than O(n²) time.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Treat the array as a linked list where the index i points to the value nums[i]. A duplicate creates a cycle.
Approaches
1. Floyd's Tortoise and Hare (Optimal) - O(N) Time, O(1) Space ⭐
Idea: Treat the array as a linked list where index -> nums[index]. A duplicate number creates a cycle.
- Phase 1: Detect the cycle. Use two pointers,
slow(moves 1 step) andfast(moves 2 steps). They meet inside the cycle. - Phase 2: Find the entry point (duplicate). Reset
slowto index 0. Move bothslowandfastone step at a time until they meet again. The meeting point is the duplicate number.
2. Brute Force Sorting - O(N log N)
Idea: Sort the array and check for adjacent equal elements. Fails the O(1) space requirement.
Solution code
import java.util.Scanner;
class Solution {
// Optimized: O(N) Time, O(1) Space - Floyd's Tortoise and Hare (Cycle Detection)
public static int findDuplicate_optimal(int[] nums) {
if (nums == null || nums.length == 0) return -1;
// Phase 1: Detect the intersection point
int slow = nums[0];
int fast = nums[nums[0]];
while (slow != fast) {
slow = nums[slow];
fast = nums[nums[fast]];
}
// Phase 2: Find the entry point (the duplicate number)
// Reset slow to the start (index 0)
slow = nums[0];
// fast remains at the intersection point
while (slow != fast) {
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
// Brute Force: O(N^2) Time, O(1) Space - Nested loops
public static int findDuplicate_brute(int[] nums) {
int N = nums.length;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if (nums[i] == nums[j]) {
return nums[i];
}
}
}
return -1; // Should not happen based on problem constraints
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read N (number of elements, which is array size)
if (!scanner.hasNextInt()) return;
int N = scanner.nextInt();
int[] nums = new int[N];
for (int i = 0; i < N; i++) {
if (!scanner.hasNextInt()) return;
nums[i] = scanner.nextInt();
}
// Use the optimized O(N) approach
int duplicate = findDuplicate_optimal(nums);
System.out.println(duplicate);
scanner.close();
}
}Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language