2

How would I check in JavaScript if an array has two or more of the SAME values in it?

For example;

['one','two','three'] -> false, because none the array items occur more than once.

['one','one','two'] -> true, as 'one' occurs more than once.

['one','one','one','two'] -> true, as 'one' occurs more than once.

['askja', 'askja', 'askja', 'iamms'] -> true, as 'askja' occurs 3 times.

Thanks in advance.

Sxribe
  • 819
  • 7
  • 16
  • 1
    Possible duplicate of [Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array](https://stackoverflow.com/questions/840781/get-all-non-unique-values-i-e-duplicate-more-than-one-occurrence-in-an-array) –  Aug 27 '19 at 00:14

4 Answers4

3

There are a few ways you can do this:

1: Use the method Stevens Qiu suggested. If the length of the array and Set aren't equal, there was a repeat in the array. This is my favorite.

2: Iterate through the array. On each iteration, add that value to a hash. If the value is already in the hash, there was a repeat so return true.

3: Iterate through the array. On each iteration get the index of (indexOf()) the item and the last index (lastIndex()) of the item. If those values aren't equal, return true.

const arr = ['hi', 'bye', 'hi'];
const arr2 = ['hi', 'bye', 'what'];

// Set size < arr length, so returns false
console.log(arr.length === new Set(arr).size);
// Set size == arr length, so returns true
console.log(arr2.length === new Set(arr2).size);
helloRupa
  • 56
  • 3
  • Thanks! This answered my question, but I forgot to ask one additional thing. How do I get the actual string that occurs twice? – Sxribe Aug 27 '19 at 12:29
  • To get the actual string, you'll be better off iterating as described in method 2 of the answer. When you find a key that already exists in the hash (Object.hasOwnProperty(key)) return the string. – helloRupa Aug 27 '19 at 21:25
1

You can iterate through your array and push each element into a Set. If there are any collisions, then you know it has identical values in it.

Stevens Qiu
  • 128
  • 1
  • 9
1

A simple solution would be to define a map that stores the existence (ie true if exists) of items values by key.

If once such item is already detected to exist via that map, you can deduce that multiple copies of a string exist in the input array:

function hasMultipleCopies(arr) {
  
  /* 
  A temporary mapping that stores "true" for an key found to exist in
  arr 
  */
  const map = {};
  
  for(const item of arr) {
    if(map[item]) {
      /* 
      If mapping contains key/value for item, then current item has
      appeared at least twice in arr 
      */
      return true;
    }
    
    map[item] = true;
  }
  
  return false;  
}

console.log(hasMultipleCopies(['one','two','three']), "=== false")
console.log(hasMultipleCopies(['one','one','two']), "=== true")
console.log(hasMultipleCopies(['one','one','one','two']), "=== true")
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
0

You can use this to get the unique values, duplicates and find out if they are duplicates.

const array = [];

const uniqueItems = [...new Set(array)]

const duplicates = array.reduce((acc, item) => {
    return uniqueItems.includes(item) ? acc : [...acc, item];
}, []);

const hasDuplicate = duplicates.length > 0;
Morlo Mbakop
  • 3,518
  • 20
  • 21