WIP

https://leetcode.com/problems/find-the-duplicate-number/
mail@pastecode.io avatar
unknown
swift
2 years ago
550 B
2
Indexable
Never
class Solution {
    func findDuplicate(_ nums: [Int]) -> Int {
        let arr = nums
        var idx = 0,
            start = 1,
            cur = arr[0],
            cnt = 0,
            N = arr.count,
            visited = 1 << N
        
        while idx < N {
            defer { idx += 1 }
            var tmp = visited | 1<<arr[idx]
            if visited == tmp {
                // NOTE: duplicated
                return arr[idx]
            } else {
                visited = tmp
            }
        }
        
        return 0
    }
}