美文网首页
数组去重

数组去重

作者: 逆风飘游的鱼 | 来源:发表于2019-08-13 21:42 被阅读0次

https://juejin.im/post/5b0284ac51882542ad774c45

双重循环、Array.prototype.indexOf()、Array.prototype.sort()、Array.prototype.reduce()、Map、Set

双重循环

双重循环去重实现比较容易。

实现一:

Array.prototype.unique = function () {

  const newArray = [];

  let isRepeat;

  for (let i = 0; i < this.length; i++) {

    isRepeat = false;

    for (let j = 0; j < newArray.length; j++) {   if (this[i] === newArray[j]) {        isRepeat = true;        break;    }

    }

    if (!isRepeat) {   newArray.push(this[i]);    }

  }

  return newArray;

}

Array.prototype.indexOf()

基本思路:如果索引不是第一个索引,说明是重复值。

实现一:

利用Array.prototype.filter()过滤功能,Array.prototype.indexOf()返回的是第一个索引值,只将数组中元素第一次出现的返回,之后出现的将被过滤掉

Array.prototype.unique = function () {

  return this.filter((item, index) => {    return this.indexOf(item) === index;  })//实现一

  this.forEach(item => {    if (newArray.indexOf(item) === -1) {      newArray.push(item);    }  });

  return newArray;实现二:

}

Array.prototype.sort()

基本思路:先对原数组进行排序,然后再进行元素比较。

实现一:

Array.prototype.unique = function () {

  const newArray = [];

  this.sort();

  for (let i = 0; i < this.length; i++) {    if (this[i] !== this[i + 1]) {      newArray.push(this[i]);    }  }//实现一

  for (let i = 0; i < this.length; i++) { if (this[i] !== newArray[newArray.length - 1]) { newArray.push(this[i])}  }

  return newArray;

}

Array.prototype.includes()

Array.prototype.unique = function () {

  const newArray = [];

  this.forEach(item => {    if (!newArray.includes(item)) {      newArray.push(item);    }  });

  return newArray;

}

Array.prototype.reduce()

Array.prototype.unique = function () {

  return this.sort().reduce((init, current) => {

    if(init.length === 0 || init[init.length - 1] !== current){     init.push(current);    }

    return init;

  }, []);

}

Map

实现一:

Array.prototype.unique = function () {

  const newArray = [];

  const tmp = new Map();

  for(let i = 0; i < this.length; i++){    if(!tmp.get(this[i])){        tmp.set(this[i], 1);     newArray.push(this[i]);   }}

    return newArray;

}

Array.prototype.unique = function () {

  const tmp = new Map();

  return this.filter(item => {    return !tmp.has(item) && tmp.set(item, 1);  })

}

Set

Array.prototype.unique =function(){constset =newSet(this);returnArray.from(set);}

Array.prototype.unique =function(){return[...newSet(this)];}

总结

除了考虑时间复杂度外、性能之外,还要考虑数组元素的数据类型(例如下面的例子)等问题权衡选择出采用哪种算法,例如:

const arr = [1, 1, '1', '1', 0, 0, '0', '0', undefined, undefined, null, null, NaN, NaN, {}, {}, [], [], /a/, /a/];

经过综合考虑,最优的数组去重算法是采用Map数据结构实现的算法。

相关文章

  • Array集结号

    实现数组去重的几种方法 数组去重一 数组去重二 利用数组indexof+push实现数组去重 数组去重三 利用对象...

  • 实现数组去重有哪些方式

    简单的数组去重 数组对象去重

  • 数组去重的四种方法

    利用双for循环去重 利用对象数组去重 利用对象数组去重并且记录重复次数 通过创建一个新数组进行数组去重

  • js数组去重、对象数组去重

    普通数组去重 一、普通数组去重 方法一:遍历数组法 方法二:排序法 方法三:对象法 对象数组去重 方法一:将对象数...

  • javascript数组去重,数组对象去重

    利用Reduce去重 function unique(arr) {var obj = {};arr = arr.r...

  • js:数组去重

    数组去重的常见写法: 数组去重封装成方法: es6的数组去重(Array.from):

  • ES6数组去重

    普通数组去重 方法1 方法2 对象数组去重

  • js reduce去重用法

    reduce不仅仅可以数据累加,还可以实现去重效果。 重复次数计算 数组去重 数组对象去重,转为数组 对象去重

  • 数组去重

    传统方法 ES6 扩展 传统方法 最后再写到 Array.prototype 原型中

  • 数组去重

    老题了。。虽然网上一搜一大堆,还是自己想了想,自己动笔写了几种。

网友评论

      本文标题:数组去重

      本文链接:https://www.haomeiwen.com/subject/txucjctx.html