JS array of methods to re-summary

Double loop for re-use to a

The simplest ideas, but low deduplication efficiency.
Thinking: comparison of the index of the outer loop and the inner loop index i is the value of j, repeated removal, and finally return an array.

	function distinct(arr){
		for(var i=0;i<arr.length-1;i++){
			for(var j=i+1;j<arr.length;j++){
				// 注意,这里i--是因为删除后,后面的值会前移
				if(arr[i] === arr[j]) {arr.splice(j--,1);}
			}
		}
		return arr;
	} 

Execution console.log(distinct([1,2,3,4,2,3,5]));
effect:[1, 2, 3, 4, 5]

De-emphasis by the double loop for two

The same low deduplication efficiency.
Ideas: the no duplicate values into a new array, and returns a new array.

	function distinct(arr){
	  var newArr=[];
	  for (var i=0;i<arr.length;i++) {
	    for (var j=i+1;j<arr.length;j++) {
	      if(arr[i]===arr[j]){++i;}
	    }
	      newArr.push(arr[i]);
	  }
	  return newArr;
	}

Execution console.log(distinct([1,2,3,4,2,3,5]));
effect:[1, 4, 2, 3, 5]

Using the indexOf () deduplication

If the array does not exist, indexOf () returns -1.
A thought: Declare an empty array, the original array traverse the judgment, if there is no new array (indexOf returns -1) to push to a new array, and returns a new array.

indexOf () returns the index of the first occurrence of the argument in the array.
Thinking two: declare an empty array, traversing the original array to determine if the i-th item in the original array in the first occurrence of the index i is not the original array, it represents the item i repeat, ignored, otherwise push to the new array before returning to New array.

	function distinct(arr){
	  var newArr=[];
	  for(var i=0;i<arr.length;i++){
	     if(newArr.indexOf(arr[i]) === -1){newArr.push(arr[i]);}
	     // if(arr.indexOf(arr[i]) === i){newArr.push(arr[i]);}
	  }
	  return newArr;
	}

Execution console.log(distinct([1,2,3,4,2,3,5]));
effect:[1, 2, 3, 4, 5]

Using the object type to achieve weight

Using the attribute name of the object can not be repeated features, high deduplication efficiency.
Ideas: attribute name of the object can not be repeated, if there are no objects on the same property name recorded in the object, if repeated in the removal of the original array, and returns the original array.

	function distinct(arr){
		var obj = {};
		for(var i=0;i<arr.length;i++){
			if(obj[arr[i]] === undefined){
				// 如果没有就添加,并且随便赋个值
				obj[arr[i]]=1;
			}else{
				// 注意,这里i--是因为删除后,后面的值会前移
				arr.splice(i--,1);
			}
		}
		return arr;
	}

Execution console.log(distinct([1,2,3,4,2,3,5]));
effect:[1, 2, 3, 4, 5]

Set to use heavy object implementation

Using the Set () default can not duplicate the characteristics of the code was minimal, but there are compatible. ... extension may be blended operator.

	function distinct(arr) {
		return Array.from(new Set(arr));
		// return [...new Set(arr)];
	}

Execution console.log(distinct([1,2,3,4,2,3,5]));
effect:[1, 2, 3, 4, 5]

Use reduce () to achieve weight

Compatible exist.
Idea: declare prev empty array, in order to determine whether there is a next prev original array, without the added push to prev, and finally return to the new array

	var arr=[1,2,3,4,2,3,5];
	var newArr = arr.reduce(function(prev,next){
		if(!prev.includes(next)) prev.push(next);
		// 下面是逻辑短路写法
		// !prev.includes(next) && prev.push(next);
	    // prev.indexOf(next) === -1 && prev.push(next);
	    return prev;
	},[]);
	console.log(newArr);

effect:[1, 2, 3, 4, 5]

Published 59 original articles · won praise 78 · views 20000 +

Guess you like

Origin blog.csdn.net/PrisonersDilemma/article/details/89539493
Recommended