URL
https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/description/?envType=daily-question&envId=2025-01-14
Code
https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202501/find_the_prefix_common_array_of_two_arrays/main.fsx
let findThePrefixCommonArray (a: int list) (b: int list) : int list =
let rec findThePrefixCommonArray' nums (freq: int[]) commons acc =
match nums with
| [] -> List.rev acc
| (a, b) :: t ->
freq.[a - 1] <- freq.[a - 1] + 1
let commons = if freq.[a - 1] = 2 then commons + 1 else commons
freq.[b - 1] <- freq.[b - 1] + 1
let commons = if freq.[b - 1] = 2 then commons + 1 else commons
findThePrefixCommonArray' t freq commons (commons :: acc)
let freq = Array.zeroCreate (List.length a)
findThePrefixCommonArray' (List.zip a b) freq 0 []