How to Check for Duplicate Strings in an Array in JavaScript – Definitive Guide

JavaScript arrays allow you to store any type of data, including strings, numbers etc.

You can check for duplicate strings in an array in JavaScript using Array.filter((item, index) => stringArray.indexOf(item) != index) statement.

Basic Example

let stringArray = ["a", "e", "i", "a", "o", "u"];

let duplicateItems = stringArray.filter((item, index) => stringArray.indexOf(item) != index);

if (duplicateItems) {
  console.log("Array contains the following duplicate elements");
  console.log(duplicateItems); // [“a”]
} else {
  console.log("Array doesn't contains duplicate elements");
}

This tutorial teaches you the different methods to check for duplicate strings in an array in JavaScript and when it is appropriate to use each method.

Using Array Filter [ES6] and IndexOf

The filter() method creates a shallow copy of the array with the items from the array that pass the test condition.

The indexOf() method returns the index of the item in the array.

This method compares the index of all the items with the index of the first occurrence of the same item and returns True or False.

Use this method when you want to check for duplicate strings in the array and create a new array with the duplicate strings.

To check for duplicate strings using the filter() method,

  • Iterate over the array using the filter() method
  • During each iteration, check if the item exists in another index using the indexOf() method
  • If yes, return the element.

Code

let stringArray = ["a", "e", "i", "a", "o", "u"];

let duplicateItems = stringArray.filter((item, index) => stringArray.indexOf(item) != index);

if (duplicateItems) {
  console.log("Array contains the following duplicate elements");
  console.log(duplicateItems); // [“a”]
} else {
  console.log("Array doesn't contains duplicate elements");
}

Using Array Some [ES6]

The some() method checks if at least one element passes the test condition specified.

This method checks each item in the array that has an index number from the beginning and is not equal to the index number from the end.

Use this method when you want to check if an array contains duplicate strings and return a boolean value directly.

To check for duplicate strings in an array using the some() function,

  • Iterate over the array using the some() function
  • Check the first index of the item and the last index of the item
  • If both indexes are not equal, the items are duplicates. It returns True
  • If both the indexes are equal, the item exists only once in the array

Code

function hasDuplicates(stringArray) {
  return stringArray.some(function(item) {
    return stringArray.indexOf(item) !== stringArray.lastIndexOf(item);
  });
}

let stringArray = ["a", "e", "i", "a", "o", "u"];

console.log(hasDuplicates(stringArray)); // true

Using Set

JavaScript set contains unique values of any type.

You can use set() to check for duplicate strings in an array.

Use this method to check if the array contains duplicate strings and create a collection with the unique elements from the array.

To check if an array contains duplicate strings using the set() method,

  • Convert the array into Set
  • Check if the length of the array is equal to the size of the set
  • If both are equal, the array doesn’t contain duplicates
  • If both sizes are different, the array contains duplicates

Code

let stringArray = ["a", "e", "i", "a", "o", "u"];

let stringSet = new Set(stringArray);

if (stringSet.size !== stringArray.length) {
  console.log("Array contains duplicate elements"); //Array contains duplicate elements
}

Additional Resources

Leave a Comment