LeetCode 2053. Kth Distinct String in an Array in F#

URL

2053. Kth Distinct String in an Array

Code

https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202408/kth_distinct_string_in_an_array/main.fsx

let kthDistinct (arr: string list) (k: int) : string =
    let rec kthDistinct' arr i m k =
        match arr with
        | [] -> ""
        | h :: t ->
            match Map.tryFind h m with
            | Some(v) ->
                if v = 1 then
                    let i' = i + 1
                    if i' = k then h else kthDistinct' t i' m k
                else
                    kthDistinct' t i m k
            | None -> failwith "never reach here"

    let m = arr |> List.countBy id |> Map.ofList
    kthDistinct' arr 0 m k