LeetCode 287. Find the Duplicate Number
URL
https://leetcode.com/problems/find-the-duplicate-number/
Code
let findDuplicate (nums: int list) : int =
let rec findDuplicate' nums left right ret =
if left > right then
ret
else
let mid = left + (right - left) / 2
let count = nums |> List.filter (fun num -> num <= mid) |> List.length
if count > mid then
findDuplicate' nums left (mid - 1) mid
else
findDuplicate' nums (mid + 1) right ret
findDuplicate' nums 1 (nums.Length - 1) 0