首页 > 代码库 > 数组去重复

数组去重复

举例 

数组  [1,1,7,4] 去重,并且去掉重复的选项为 [7,4]

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<title>Document</title>
</head>
<body>
	<script>
		Array.prototype.unique3 = function(){
		 var res   =  [];
		 var json  =  {};
		 var count =  0 ;
		 var obj   =  {};
		 for(var i = 0; i < this.length; i++){
		    if(!json[this[i]]){  //未存
		       res.push(this[i]);
		       json[this[i]] = ++count;
		    }else{               //已存
		    	if(!obj[this[i]]){ //首次
		    		obj[this[i]] = 1;
		    		for(var j=0;j<res.length;j++){
		    			if(res[j]==this[i]){
		    				res.splice(j,1)
		    			}
		    		}		    		
		    	}
		    }
		 }
		 return res;
		}
		var arr = [1,1,7,4,7];
		console.log(arr.unique3());	
	</script>
</body>
</html>

  

数组去重复