JS除重,删除重复元素


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS除重,删除重复元素</title>


<script type="text/javascript">  
(function(){
////////////////////////////////////////
    var a=[1,2,1,3,1,2,5,6,7,6,5];
    document.write(a+'<hr>');
    document.write(keepDifferent(a));
    console.log(a);
    console.log(keepDifferent(a));
    
    function keepDifferent(arr){
        var x=[];
        for (var i=0; i<arr.length; i++) {  
            if(!inArray(arr[i],x)){
                x.push(arr[i]);
            }
        }
        return x;
    }
    
    function inArray(x,arr){
        for (var i=0; i<arr.length; i++) {  
            if(arr[i]==x){
                return true;
            }
        }
        return false;
    }

////////////////////////////////////////
})();
</script>


程序运行结果如下,点击这里亲自运行看看