-1

Something like this:

let a = [2, 34, 'dafsd', null, {}];
let b = [null,  null] -or- ['same','same'] -or- [100, 100]
isDistinct(a) // => true
isDistinct(b) // => false
Suman Kundu
  • 1,702
  • 15
  • 22
  • 1
    What have you tried? What specifically do you need help with? – Carcigenicate Jun 25 '18 at 15:05
  • 2
    If you used a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) you don't have to worry about duplicates. – Patrick Evans Jun 25 '18 at 15:07
  • 2
    Why should `isDistinct([{}, {}])` return `false`? It’s two distinct objects. – Sebastian Simon Jun 25 '18 at 15:07
  • Possible duplicate of [In Javascript, how do I check if an array has duplicate values?](https://stackoverflow.com/questions/7376598/in-javascript-how-do-i-check-if-an-array-has-duplicate-values) – Pac0 Jun 25 '18 at 16:08
  • @Xufox sure they are different, my bad that I considered objects too, as that worth a separate question itself. – Suman Kundu Jun 26 '18 at 04:19

1 Answers1

4

You could take a Set of the items and check the length of the array against the size of the set. If equal, then all elements are unique.

let a = [2, 34, 'dafsd', null, {}];

console.log(a.length === new Set(a).size);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 2
    As an aside, just in case it's not clear to OP, Set definitely doesn't take into account the innards of objects. a.e. `new Set([ {a:1}, {a:1} ])` would return two objects. This is correct, but often confusing if you're newer to the language. – zfrisch Jun 25 '18 at 15:12