Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 26. Remove Duplicates from Sorted Array in F#

Shohei Yoshida's photo
Shohei Yoshida
·Nov 11, 2022·

1 min read

URL

leetcode.com/problems/remove-duplicates-fro..

Code

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

let removeDuplicates (nums: int []) : int =
    let rec removeDuplicates' i prev pos (nums: int []) =
        if i >= nums.Length then
            pos
        else if prev = nums.[i] then
            removeDuplicates' (i + 1) prev pos nums
        else
            nums.[pos] <- nums.[i]
            removeDuplicates' (i + 1) nums.[i] (pos + 1) nums

    removeDuplicates' 1 nums.[0] 1 nums
 
Share this