Skip to content

Instantly share code, notes, and snippets.

@Linkding
Last active May 13, 2017 03:16
Show Gist options
  • Save Linkding/66936102c502a11373a2c6930b9b77e3 to your computer and use it in GitHub Desktop.
Save Linkding/66936102c502a11373a2c6930b9b77e3 to your computer and use it in GitHub Desktop.
26.js
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
//indexOf,利用这个方法,获取某个元素在数组中的位置;
len = nums.length;
a = [];
for (i = 0; i < len; i ++)
{
ele = nums[i];
//console.log(ele)
index = nums.indexOf(ele);
if (index != i)
{
a.push(i);
}
}
//console.log(a)
//去掉多余的元素;
len2 = a.length
//判断如果数组不存在重复的元素,返回原数组len,并终止;
if (len2 == 0)
{
return len;
//return;
}
else
{
for (j = len2-1; j >= 0; j --)
{
nums.splice(a[j],1);
}
//算新的len
new_len = nums.length;
return new_len;
}
};
console.log(removeDuplicates([-3,-2,2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment