码迷,mamicode.com
首页 > 编程语言 > 详细

Remove Duplicates from Sorted Array by Javascript

时间:2017-09-05 16:55:40      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:logs   sort   prototype   console   color   out   not   obj   pre   

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

标签:logs   sort   prototype   console   color   out   not   obj   pre   

原文地址:http://www.cnblogs.com/calvinzqz/p/7479140.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!