Implement an array of methods to re-organize

1. The two-cycle deduplication

Double for (or while) loop method is relatively early, if the length of the array is large, it will be very memory-intensive

    function setArray(arr) {
    if (Array.isArray(arr)) {
        let res = [arr[0]]
	    for (let i = 1; i < arr.length; i++) {
	        let flag = true
	        for (let j = 0; j < res.length; j++) {
	            if (arr[i] === res[j]) {
	                flag = false;
	                break
	            }
	        }
	        if (flag) {
	            res.push(arr[i])
	        }
	    }
	     return res
	    }
	}

2.indexOf approach to weight

(1) The first method defines an empty array res, and then call the original method indexOf traverse the array is determined, if the element is not in res, which will push into the res, the return res

  function setArray1(arr) {
    if (Array.isArray(arr)) {
        let res = []
	    for (let i = 0; i < arr.length; i++) {
	        if (res.indexOf(arr[i]) === -1) {
	            res.push(arr[i])
	        }
	    }
	    return res
	    }
}

If (2) by the position detector element in the array indexOf first occurrence of the element and the current position is equal to, if not equal repeating element indicates that the element is

 function setArray2(arr) {
    if (Array.isArray(arr)) {
        return Array.prototype.filter.call(arr, function(item, index){
           return arr.indexOf(item) === index;
       });
    } 
}

3. Neighbors deduplication

First sorted and then traverse the sorted according to the result and the ratio of pairs of adjacent elements, is skipped if the change elements are equal until the end of traversal

 function setArray(arr) {
    if (Array.isArray(arr)) {
        arr = arr.sort()
	    let res = []
	    for (let i = 0; i < arr.length; i++) {
	        if (arr[i] !== arr[i-1]) {
	            res.push(arr[i])
	        }
	    }
	    return res
    }
}

4. The use of object properties deduplication

Create an empty object through the array, the array is set to the value of the object's properties, and the properties assigned to the initial value of 1, each appeared once, the corresponding property value increased by 1, so is the number of times the element can occur corresponding property value the

  function setArray(arr) {
    if (Array.isArray(arr)) {
          let res = [],
          obj = {}
		  for (let i = 0; i < arr.length; i++) {
		      if (!obj[arr[i]]) {
		          res.push(arr[i])
		          obj[arr[i]] = 1
		      } else {
		          obj[arr[i]]++
		      }
		  }
		  return res
    }
}

5.new Set

Set the new new de-duplication, then the Set Assignment Array.from structure or structures into an array

  function setArray(arr) {
    if (Array.isArray(arr)) {
       return [...new Set(arr)]
    }
    
}

or

  function setArray(arr) {
    if (Array.isArray(arr)) {
      return Array.from(new Set(arr))
    }  
}

6. Use includes

  function setArray(arr){
      if(Array.isArray(arr)){
           var array =[];
		   for(var i =0; i <arr.length;i++){           
		       if(!array.includes(arr[i])){//includes 检测数组是否有某个值
		          array.push(arr[i]);
		       }
		    }
		return array
      }
} 
 //{}没有去重

7. Use the filter

  function setArray(arr){
    return arr.filter( function(item, index,arr){//当前元素,在原始数组中的第一个索引==当前索引值,否则返回当前元素
           return arr.indexOf(item,0)===index;
     });
  }

8. Map data structure using deduplication

  function setArray(arr){
    let map = new Map();
    let array = new Array();// 数组用于返回结果
    for(let i =0;i <arr.length;i++){
        if(map .has(arr[i])){// 如果有该key值
            map.set(arr[i],true);
        }
        else{
            map .set(arr[i],false);// 如果没有该key值
            array.push(arr[i]);
        }
  }
 return array ;
}

9. Using reduce

Array will need to re-processing of the current item to find in the array initialization, if not, it will continue to add to the array initialization and returns an array initialization

  var newArr = arr.reduce(function (prev, cur) {
    prev.indexOf(cur) === -1 && prev.push(cur);
    return prev;
 },[]);

Of course, there may also be implemented recursively, personally feel no need to.

Published 15 original articles · won praise 2 · Views 415

Guess you like

Origin blog.csdn.net/goUp_self/article/details/100702047