JS's four array deduplication methods

I went to the interview today. Others have such a question, give a js array, and then remove the repeated elements in the array. At that time, I didn't think of a good method, so I used the method of looping and looping to do it directly. Now I searched the Internet and found that there are better methods. There are four kinds of methods in total:

 

function getArray(){
		var arr = [];
		for(var i = 0; i < 10000; i++){
			arr[i] = (Math.random() * 100);
		}
		console.log(arr);
		return arr;
	};
	
	//This method is a pure loop set of loops
	function unique1(array) {
		var starttime = new Date().getUTCMilliseconds();
		var newArray = [];
		var len = array == undefined ? 0 : array.length;
		for (var i = 0; i <len; i ++) {
			var count = 0;
			for(var j = 0; j < newArray.length; j++) {
				if(newArray[j] == array[i]) {
					count++;
					break;
				}
			}
			if(count == 0){
				newArray.push(array[i]);
			}
		}
		var endtime = new Date().getUTCMilliseconds();
		console.log("1---------耗时-------"+(endtime-starttime));
		return newArray;
	}
	//The method uses the hash table. Store the ones that have already appeared in an object in the form of subscripts. Subscripted references are much faster than searching the array with indexOf
	function unique2(array) {
		var starttime = new Date().getUTCMilliseconds();
		var newArray = [];
		var hash = {};//As a hash table
		var len = array == undefined ? 0 : array.length;
		for (var i = 0; i <len; i ++) {
			if(!hash[array[i]]){
				newArray.push(array[i]);
				hash[array[i]] = true;//Store the subscripted form of the array elements in the hash table
			}
		}
		var endtime = new Date().getUTCMilliseconds();
		console.log("2---------耗时-------"+(endtime-starttime));
		return newArray;
	}
	
	//The purpose of this method is to use indexOf to find the first occurrence of the stored parameter in the array. Obviously, when the js engine implements this method, it will traverse the array until it finds the target. So this function will waste a lot of time.
	function unique3(array) {
		var starttime = new Date().getUTCMilliseconds();
		var newArray = [];
		var len = array == undefined ? 0 : array.length;
		for (var i = 0; i <len; i ++) {
			if(newArray.indexOf(array[i]) == -1){
				newArray.push(array[i]);
			}
		}
		var endtime = new Date().getUTCMilliseconds();
		console.log("3---------耗时-------"+(endtime-starttime));
		return newArray;
	}
	
	// Sort first, compare two adjacent elements, and store them in a new array if they are different, and they will not be stored if they are the same.
	function unique4(array) {
		var starttime = new Date().getUTCMilliseconds();
		var newArray = [];
		var len = array == undefined ? 0 : array.length;
		if(len > 0) {
			array.sort();
		}
		newArray[0] = array[0];
		for (var i = 1; i <len; i ++) {
			if(array[i] != array[i-1]){
				newArray.push(array[i]);
			}
		}
		var endtime = new Date().getUTCMilliseconds();
		console.log("3---------耗时-------"+(endtime-starttime));
		return newArray;
	}
	
	var array = getArray();
	unique1(array);
	unique2(array);
	unique3(array);
	unique4(array);

 

In my own test, I always feel that the second type is the most efficient, and I don't know what other great gods have any good opinions.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326851576&siteId=291194637