日期:2014-05-16  浏览次数:20340 次

JavaScript数组去重(数组去掉重复元素)

<script>
/**
 * 利用JavaScript内置的hash去重数组中的重复元素
 * @author kanpiaoxue
 * date 2011/08/29 13:29:01
 */
function uniqueArray(arr){
	if(null == arr || arr.length == 0 ){
		return arr;
	}else{
		var innerHashMap = {};
		for(var i = 0, j = arr.length; i<j; i++){
			innerHashMap[arr[i]] = null;
		}
		var rs = [];
		for (obj in innerHashMap){
			rs.push(obj);
		}
		return rs;
	}
}

/*------------------------ test ---------------------------*/
var arr = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0];
var arr1 = [];
for(var i = 0 ; i < 10; i++){
	arr1[i] = 'element' + (i%2);
}

//alert('source: ' + arr + '\n' + 'target: ' + uniqueArray(arr) + '\n' + 'source: ' +  arr1 + '\n' + 'target: ' +  uniqueArray(arr1))

var longArr = []
for(var i = 0; i < 100000; i++){
	longArr[i] = i;
}

var start = (new Date()).getTime();
uniqueArray(longArr);
var end = (new Date()).getTime();
alert((end - start) + 'ms');

</script>
?