Giter Site home page Giter Site logo

Comments (20)

CatBone avatar CatBone commented on July 29, 2024 6

还一种简单的,可以运用es6的扩展运算符
var unique = a => [...new Set(a)]

😝😝😝

from underscore-analysis.

Arvinzhu avatar Arvinzhu commented on July 29, 2024 2

@HowardTangHw 没有中断外部循环,而是内部循环在遇到充负的元素时,修改i的值,也就是修改外部循环下次循环的起点,当内部循环执行完毕后,push一个没有重复的值到res中,然后外部循环执行 以修改后的i值执行i++开始执行下一次循环

from underscore-analysis.

seaskymonster avatar seaskymonster commented on July 29, 2024 1

对于 1 和 "1" 无法分别这个问题:

其实可以在你的方法四上直接改
function unique(a) { var seen = {}; return a.filter(function(item){ return seen.hasOwnProperty(typeof(item)+item) ? false: (seen[typeof(item)+item] = true); }) }

from underscore-analysis.

idda avatar idda commented on July 29, 2024

学习了

from underscore-analysis.

silasFFF avatar silasFFF commented on July 29, 2024

1024

from underscore-analysis.

L9m avatar L9m commented on July 29, 2024

学习了

from underscore-analysis.

Wangbaogang avatar Wangbaogang commented on July 29, 2024

赞赞赞
-- 来自点赞小能手

from underscore-analysis.

aleen42 avatar aleen42 commented on July 29, 2024

方法三如果用严格判定 item !== arr[pos - 1] 的话,不就能解决 Number 与 String 排在一起的问题咯?

from underscore-analysis.

BigKongfuPanda avatar BigKongfuPanda commented on July 29, 2024

厉害,没想到有这么多种方法呢

from underscore-analysis.

BigKongfuPanda avatar BigKongfuPanda commented on July 29, 2024

我想咨询一个问题,如果说数组是这样:arr = [{name:"tom"},{name:"tom"},{age: 18}]; 请问数组中的第一项和第二项,是算重复的嘛? 如果是的话,你上面的方法好像失效了。

from underscore-analysis.

aleen42 avatar aleen42 commented on July 29, 2024

@BigKongfuPanda 在作者看来,这种情况应该属于引用不同,因此不能称作完全相同的两个元素。这在于你如何理解两个元素何谓重复。

from underscore-analysis.

BigKongfuPanda avatar BigKongfuPanda commented on July 29, 2024

from underscore-analysis.

tiansn avatar tiansn commented on July 29, 2024

@hanzichi ,能分析下,每种方法的复杂度么,特别是最后的那几种

from underscore-analysis.

zhangbowei avatar zhangbowei commented on July 29, 2024

function unique(a) {
return Array.from(new Set(a));
}

var a = [{name: "hanzichi"}, {name: "hanzichi"},{age: 30}, new String(1), new Number(1)];
var ans = unique(a);
console.log(JSON.stringify(ans));
//output
[{"name":"hanzichi"},{"name":"hanzichi"},{"age":30},"1",1]

from underscore-analysis.

MarioGogogo avatar MarioGogogo commented on July 29, 2024

学习了

from underscore-analysis.

hms181231 avatar hms181231 commented on July 29, 2024

学习了

from underscore-analysis.

HowardTangHw avatar HowardTangHw commented on July 29, 2024

您好,我有个疑问,为什么在方法2中j=++i会把外部的循环中断掉,而不执行语句res.push(a[i])呢?

function unique(a) {
  var res = [];

  for (var i = 0, len = a.length; i < len; i++) {
    for (var j = i + 1; j < len; j++) {
      if (a[i] === a[j]) {
        console.log('中断了')
        j = ++i;
      }
    }
    // 不清楚为啥j=++i;会break掉循环,不进入这一步
    console.log('没被中断');
    res.push(a[i]);
  }

  return res;
}

var a = [1, 1, "1", "2", 1];
var ans = unique(a);
console.log(ans); // => ["1", "2", 1]

from underscore-analysis.

islishude avatar islishude commented on July 29, 2024

indexOf 最大的问题就是 NaN 无法去重

from underscore-analysis.

wangliang1124 avatar wangliang1124 commented on July 29, 2024

方法三: return !pos || item != ary[pos - 1]; 换成 return !pos || item !== ary[pos - 1]; 使用严格相等应该就行了吧

from underscore-analysis.

wangliang1124 avatar wangliang1124 commented on July 29, 2024

_20180328161606

from underscore-analysis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.