Skip to content

Commit

Permalink
Merge pull request neetcode-gh#850 from t3chkid/main
Browse files Browse the repository at this point in the history
Adds solution for 287. Find the Duplicate Number in Kotlin
  • Loading branch information
Ahmad-A0 authored Aug 18, 2022
2 parents a3e3549 + 31a8642 commit 191c61c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions kotlin/287-Find-The-Duplicate-Number.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package kotlin

class Solution {
fun findDuplicate(nums: IntArray): Int {
var slow = 0
var slow2 = 0
var fast = 0
// detect the cycle
while (true) {
slow = nums[slow]
fast = nums[nums[fast]]
if (slow == fast) break
}
// get the first value at the beginning of the cycle
while (true) {
slow2 = nums[slow2]
slow = nums[slow]
if (slow2 == slow) return slow
}
}
}

0 comments on commit 191c61c

Please sign in to comment.