LeetCode 2870. Minimum Number of Operations to Make Array Empty in F#

URL

https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/description/

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/problems/2870/main.fsx

let minOperations (nums: int list) : int =
    let freq =
        nums
        |> List.fold (fun acc n -> Map.add n ((Map.tryFind n acc |> Option.defaultValue 0) + 1) acc) Map.empty

    if Map.values freq |> Seq.tryFind ((=) 1) |> Option.isSome then
        -1
    else
        Map.values freq
        |> Seq.fold (fun acc n -> if n % 3 = 0 then acc + (n / 3) else acc + (n / 3) + 1) 0