【LeetCode】解析_26. Remove Duplicates from Sorted Array

26. Remove Duplicates from Sorted Array

難易度:Easy 

參考及出處網站:Leetcode

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

思路

這題主要是去抓一個陣列有幾個不同數值
其實也就只用排序 

My C# Solution

public class Solution {
    public int RemoveDuplicates(int[] nums) {
    if (nums.Length > 1)
        {
            var count = 1;
            for(var i = 1; i < nums.Length; i++)
            {
                if (nums[i - 1] != nums[i])
                {
                    nums[count] = nums[i];
                    count++;
                }
            }            
            return count;            
        }
        return nums.Length;        
    }
}

結果: