DEV Community

Omiye Precious
Omiye Precious

Posted on

How to Remove Duplicate Values from JavaScript Arrays and Strings

Consider a situation where you are developing a JavaScript application, but somehow it keeps duplicating like a rabbit family. It's annoying. Be it repeated names in a user list or repeated words in a sentence. It is indispensable to delete such redundant data from the entire system so that one does not have to suffer from performance and accuracy.

This guide will show you the best ways to remove duplicates from arrays and strings in JavaScript. There will be efficient methods, edge cases, and their applications in the real world. Let's clean them up!

Removing Duplicates from Arrays

Arrays are lovers of duplicates despite thrashing them out: Let's see how to kick them out.

Method 1: Using The Quick Fix

A Set object is a stringent bouncer at this club—no duplicates allowed!

const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = [...new Set(numbers)];
console.log(uniqueNumbers); // [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

What does it offer? Simple speed, efficiency, and visibility. Downside: It's for primitives (strings, numbers, booleans) only.

Method 2: Using The Classic Approach

So, Set is a strict bouncer; filter() and indexOf() are detectives in tracking down duplicates.

const words = ["apple", "banana", "apple", "orange", "banana"];
const uniqueWords = words.filter((item, index) => words.indexOf(item) === index);
console.log(uniqueWords); // ["apple", "banana", "orange"]
Enter fullscreen mode Exit fullscreen mode

What does it offer? It works for primitive values. Downside: Slower than Set, especially when applied to large arrays.

Method 3: Using The Power User’s Choice

reduce() is a sweet Swiss army knife; it offers a lot of things but can be a little tricky sometimes.

const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = numbers.reduce((acc, curr) => {
  if (!acc.includes(curr)) acc.push(curr);
  return acc;
}, []);
console.log(uniqueNumbers); // [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

What does it offer? More control over the output, that is less intuitive as compared to Set or filter().

Removing Duplicates from Strings

Processing strings is removing duplicate characters or words at times.

Method 1

Strings can easily be converted into arrays, hence, it works with Set also.

const str = "hello world";
const uniqueStr = [...new Set(str)].join("");
console.log(uniqueStr); // "helo wrd"
Enter fullscreen mode Exit fullscreen mode

It is fast and easy, but the big minus is that it will remove the space, which one may not want sometimes.

Method 2

For removing duplicate words rather than duplicate characters:

const sentence = "JavaScript is fun and JavaScript is powerful";
const uniqueSentence = [...new Set(sentence.split(" "))].join(" ");
console.log(uniqueSentence); // "JavaScript is fun and powerful"
Enter fullscreen mode Exit fullscreen mode

It is good because it works with words and not with characters only, but it is case-sensitive ("JavaScript" and "javascript" are two different words).

Handling Edge Cases

Let us now talk about tricky issues:

  • Mixed data types in arrays: Set will remove duplicates but cannot deep-compare objects.
  • Case sensitivity: "apple" and "Apple" are two different values in JavaScript.
  • Empty arrays and empty strings: Always check if these exist before one starts running the operations.

Real-World Applications

What possible help can it render in actual projects?

  • User lists: Remove duplicates in names because multiple entries will not get the user signed up.
  • Search filters: Ensure that search queries are unique.
  • Data sanitization: Keep redundant API responses or entries in the database to a minimum.

Conclusion

Duplicate data is an unavoidable mess, but the means we've now learned will go a long way to helping us fight it. Use Set for quick fixes, filter() for more control, and reduce() when custom handling is necessary. Just keep performance in your sights, and your JavaScript code will continue to be efficient and clean.

Top comments (0)