题解 | #数组去重# Set方法和reduce方法

数组去重

http://www.nowcoder.com/practice/0b5ae9c4a8c546f79e2547c0179bfdc2

使用Set集合:

Array.prototype.uniq = function () {
    return Array.from(new Set(this));
}

reduce+includes方法:

Array.prototype.uniq = function () {
  return this.reduce((prev, cur) => {
    return prev.includes(cur) ? prev : [...prev, cur];
  }, []);
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务