Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 645. Set Mismatch in F#

Shohei Yoshida's photo
Shohei Yoshida
·Oct 23, 2022·

1 min read

URL

leetcode.com/problems/set-mismatch

Code

github.com/syohex/dotnet-study/blob/master/..

let findErrorNums (nums: int list) : (int * int) =
    let rec findErrorNums' nums duplicated acc =
        match nums with
        | [] -> duplicated, (Array.findIndex (fun n -> n = 0) acc) + 1
        | h :: t ->
            acc.[h - 1] <- acc.[h - 1] + 1

            if acc.[h - 1] = 2 then
                findErrorNums' t h acc
            else
                findErrorNums' t duplicated acc

    findErrorNums' nums -1 (Array.zeroCreate nums.Length)
 
Share this