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.

1

5 Answers 5

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This answered my question, but I forgot to ask one additional thing. How do I get the actual string that occurs twice?
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.
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.

Comments

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")

Comments

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;

Comments

0

This simply checks for duplicates in an array using Set introduced in ES2015 spec

const duplicatesValueExist = (values) => new Set(values).size !== values.length;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.