xxxxxxxxxx
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
};
xxxxxxxxxx
// Java
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
xxxxxxxxxx
public int RemoveDuplicates(int[] nums)
{
int i=0,j=1;
while(j<nums.Length)nums[++i]=nums[j++]==nums[--i] ? nums[i]:nums[j+i-++i];
return i+1;
}
i am freindly code below
public class Solution
{
public int RemoveDuplicates(int[] nums)
{
int i=0,j=1;
while(j<nums.Length)
{
if(nums[j]==nums[i]) j++;
else nums[++i] =nums[j++];
}
return i+1;
}
}
xxxxxxxxxx
def remove_duplicates(nums: [int]) -> int:
cnt = 1
for index in range(len(nums) - 1):
if nums[index] != nums[index + 1]:
nums[cnt] = nums[index + 1]
cnt += 1
print(cnt)
xxxxxxxxxx
def remove_duplicate(nums: [int]) -> int:
nums[:] = sorted(set(nums))
return len(nums)
xxxxxxxxxx
function removeDuplicates(&$nums)
{
$count = count($nums);
if (!$count) {
return 0;
}
$i = 0;
for ($j = 1; $j < $count; $j++) {
if ($nums[$j] !== $nums[$i]) {
$i++;
$nums[$i] = $nums[$j];
}
}
return ++$i;
}