Remove Duplicates from Sorted Array by Javascript
阿新 • • 發佈:2017-09-05
repeat var log moved class push length cat bject
Solution A:
1.Create a array store the result.
2.Create a object to store info of no- repeat element.
3.Take out the element of array, and judge whether in the object. If not push into result array.
Array.prototype.removeDuplicates = function(){ var res = []; var js = {}; for(var i = 0; i < this.length; i++){ console.log("**:" + i + " is :" + js[this[i]]); console.log(js); if(!js[this[i]]){ console.log(this[i]); res.push(this[i]); js[this[i]] = 1; } } return res; } var arr = [1,2,3,4,3,3,43,4,5,6,4,4,3,54,6,6]; arr.removeDuplicates();
Remove Duplicates from Sorted Array by Javascript