1. Double cycle

Implementation Method 1

function removeRepeat(arr) {
    let newArr = [];
    for(let i = 0; i <= arr.length; i++) {
        let isRepeat = false;
        for(let j = 0; j <= newArr.lenght; j++) {
            if (arr[i] === newArr[j]) {
                isRepeat = true;
                break;// Break the loop}}if(!isRepeat) {
            newArr.push(arr[i]);
        }
    }
    return newArr;
}
Copy the code

Implementation Method 2

function removeRepeat(arr) {
    let newArr = [];
    for(let i = 0; i < arr.length; i++) {
        let isRepeat = false;
        for(let j = i + 1; j < arr.length; j++) {
            if (arr[i] === arr[j]) {
                isRepeat = true;
                break;// Break the loop}}if(!isRepeat) {
            newArr.push(arr[i]);
        }
    }
    return newArr;
}
Copy the code

Implementation Method 3

function removeRepeat(arr) {
    let newArr = [];
    for(let i = 0; i < arr.length; i++) {
        for(let j = i + 1; i < arr.length; i++) {
            if (arr[i] === arr[j]) {
                j = ++i; // increment before assignment}}}return newArr;
}
Copy the code

2.Array.prototype.indexOf

The filter and indexOf arrays are duplicates if they are not the first index.

Implement a:

  • Filter through the array filter function
  • IndexOf returns the first index value
  • An index value equal to the current value is filtered into the array
 function removeRepeatFour(arr) {
      return arr.filter((item, index) = > {
        returnarr.indexOf(item) === index; })}let test = removeRepeatFour(allArr);
Copy the code

Implementation method two:

function removeRepeat (arr) {
  let newArray = [];
  arr.forEach(item= > {
    if (newArray.indexOf(item) === -1) { newArray.push(item); }});return newArray;
}
Copy the code

3.Array.prototype.sort()

Basic idea: after the array is arranged, judge

Implementation method 1:

function removeRepeat (newArr) {
  const newArray = [];
  let arr = newArr.slice(0);// Implement a clone to prevent the impact of the original array, which contains the reference data type, need a deep copy
  arr.sort();
  for (let i = 0; i < arr.length; i++) {
    if(arr[i] ! == arr[i +1]) { newArray.push(arr[i]); }}return newArray;
}
Copy the code

Implementation method two:

function removeRepeat(newArr) {
  const newArray = [];
  let arr = newArr.slice(0);// Implement a clone to prevent the impact of the original array, which contains the reference data type, need a deep copy
  arr.sort();
  for (let i = 0; i < arr.length; i++) {
    if(arr[i] ! == newArray[newArray.length -1]) { newArray.push(arr[i]); }}return newArray;
}
Copy the code

4.Array.prototype.includes()

function removeRepeat(arr) {
  const newArray = [];
  arr.forEach(item= > {
    if (!newArray.includes(item)) {
      newArray.push(item);
    }
  });
  return newArray;
}
Copy the code

5.Map

Implementation method 1:

function removeRepeat (arr) {
  const newArray = [];
  const tmp = new Map(a);for(let i = 0; i < arr.length; i++){
        if(! tmp.get(arr[i])){ tmp.set(arr[i],1); newArray.push(arr[i]); }}return newArray;
}
Copy the code

Implementation method two:

function removeRepeat(arr) {
  const tmp = new Map(a);return arr.filter(item= > {
    return! tmp.has(item) && tmp.set(item,1); })}Copy the code

Set

Array.prototype.unique = function () {
  return [...new Set(this)];
}
Copy the code

Note: this article is only used for personal notes, simply to sort out the knowledge for their own review.