Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)
Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)

🔧 Programmierung 🕛 vor 3 Monaten 3 Min Lesezeit
0

Find the Duplicate Number

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




Problem Statement



Given an array nums containing n + 1 integers where:




  • Each integer is in the range [1, n]

  • There is only one repeated number

  • Return that duplicate number






Example






CODE
Input: nums = [1,3,4,2,2]
Output: 2












Brute Force Approach






Interview Explanation



The most straightforward solution is to compare every element with every other element. If two numbers are equal, we have found the duplicate.



Since we check all possible pairs, the duplicate is guaranteed to be found.






Time & Space Complexity




  • Time Complexity: O(N²)

  • Space Complexity: O(1)






Java Code






CODE
class Solution {
public int findDuplicate(int[] nums) {

for(int i = 0; i < nums.length; i++) {
for(int j = i + 1; j < nums.length; j++) {
if(nums[i] == nums[j]) {
return nums[i];
}
}
}

return -1;
}
}












Better Approach – HashSet






Interview Explanation



Instead of repeatedly searching for duplicates, we can store already seen numbers inside a HashSet.



Before inserting a number, check whether it already exists in the set.



If it does, that's our duplicate.






Why HashSet?



HashSet provides nearly O(1) lookup time, eliminating the need for nested loops.






Time & Space Complexity




  • Time Complexity: O(N)

  • Space Complexity: O(N)






Java Code






CODE
class Solution {
public int findDuplicate(int[] nums) {

HashSet<Integer> seen = new HashSet<>();

for(int num : nums) {
if(seen.contains(num)) {
return num;
}

seen.add(num);
}

return -1;
}
}












Optimal Approach – Floyd's Cycle Detection



Most interviewers won't stop at HashSet.



They'll ask:




Can you solve it in O(1) extra space?




This is where the real trick begins.









Observation



Every number in the array lies between 1 and n.



Think of each value as a pointer to the next index.




CODE
index -> nums[index]






For:




CODE
nums = [1,3,4,2,2]






we get:




CODE
0 -> 1
1 -> 3
3 -> 2
2 -> 4
4 -> 2






Visualized:




CODE
0 → 1 → 3 → 2 → 4
↑ ↓
← ← ←






A cycle appears.



And what creates this cycle?



The duplicate number.



This transforms the problem into:




Find the starting point of the cycle in a linked list.




Exactly what Floyd's Algorithm does.









Phase 1: Detect the Cycle



Use two pointers:




  • Slow moves one step

  • Fast moves two steps




CODE
slow = nums[slow];
fast = nums[nums[fast]];






Eventually they meet inside the cycle.









Phase 2: Find the Duplicate



Move one pointer back to the beginning.



Now move both one step at a time.



Where they meet again is the duplicate number.









Dry Run






Input






CODE
nums = [1,3,4,2,2]









Initial State






CODE
slow = 1
fast = 1









Iteration 1






CODE
slow = nums[1] = 3

fast = nums[nums[1]]
= nums[3]
= 2









Iteration 2






CODE
slow = nums[3] = 2

fast = nums[nums[2]]
= nums[4]
= 2






Pointers meet at:




CODE
2






Cycle detected.









Reset Slow






CODE
slow = nums[0]






Move both one step at a time:




CODE
slow = nums[slow]
fast = nums[fast]






They meet again at:




CODE
2






This is the duplicate number.









Optimal Java Solution






CODE
class Solution {
public int findDuplicate(int[] nums) {

int slow = nums[0];
int fast = nums[0];

do {
slow = nums[slow];
fast = nums[nums[fast]];
} while (slow != fast);

slow = nums[0];

while (slow != fast) {
slow = nums[slow];
fast = nums[fast];
}

return slow;
}
}












Complexity Analysis






Time Complexity




  • Phase 1: O(N)

  • Phase 2: O(N)



Overall:




CODE
O(N)









Space Complexity






CODE
O(1)






No extra data structures are used.









Interview Takeaway



Whenever you see:




  • Array values limited to a range

  • Numbers acting like references

  • Need to find duplicates

  • O(1) space requirement



Think:




CODE
Index = Node
Value = Next Pointer






The moment you see that transformation, the problem becomes:




Find the entrance of a cycle in a linked list.




And Floyd's Tortoise and Hare algorithm solves it beautifully.



GitHub:



Medium: https://medium.com/@jaspreet.dev

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
Use custom web fonts in Google Sheets charts
2 Quellen
Introducing the new 1Password App for Google Chat
1 Quelle
Context-aware access controls are available for Gemini Enterprise in the Admin console
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Find the Duplicate Number

Thematisch verwandte Begriffe: Find, Duplicate, Number · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...