LeetCode 2965. Find Missing and Repeated Values in F#

URL

Find Missing and Repeated Values - LeetCode

Code

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

let findMissingAndRepeatedValues (grid: int[,]) : int * int =
    let len = Array2D.length1 grid

    grid
    |> Seq.cast<int>
    |> Seq.fold
        (fun acc n ->
            let v = Map.tryFind n acc |> Option.defaultValue 0
            Map.add n (v + 1) acc)
        Map.empty
    |> fun m ->
        seq { 1 .. (len * len) }
        |> Seq.fold
            (fun (a, b) i ->
                match Map.tryFind i m with
                | None -> a, i
                | Some v when v = 2 -> i, b
                | _ -> a, b)
            (0, 0)