LeetCode 287. Find the Duplicate Number

URL

https://leetcode.com/problems/find-the-duplicate-number/

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202309/find_the_duplicate_number/main.fsx

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