Remove Duplicates from Sorted Array by Javascript

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();

 

posted @ 2017-09-05 16:08  JadeofMoon  阅读(75)  评论(0编辑  收藏  举报