Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 219. Contains Duplicate II in F#

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

1 min read

URL

leetcode.com/problems/contains-duplicate-ii

Code

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

let containsNearByDuplicate (nums: int list) (k: int) : bool =
    let rec containsNearByDuplicate' nums i k acc =
        match nums with
        | [] -> false
        | h :: t ->
            match Map.tryFind h acc with
            | None -> containsNearByDuplicate' t (i + 1) k (Map.add h i acc)
            | Some (prev) ->
                if i - prev <= k then
                    true
                else
                    containsNearByDuplicate' t (i + 1) k (Map.add h i acc)

    containsNearByDuplicate' nums 0 k Map.empty
 
Share this