PROPERLY Remove Duplicates from Array in JS [SOLVED]


JavaScript

Introduction

In the ever-evolving world of web development, JavaScript has become an indispensable tool, powering countless applications and websites. One common challenge faced by developers is to efficiently manage and manipulate data, and this includes handling duplicate entries in an array. In this article, we delve into various methods to remove duplicates from array JS, allowing you to optimize your code and improve overall performance.

JavaScript's dynamic nature allows for an array to hold various types of data, which can sometimes lead to the creation of duplicate entries. This issue, if left unaddressed, can affect the accuracy of data processing and hinder the proper functioning of applications. As a result, it is vital for developers to be familiar with different techniques to remove duplicates from array JS, ensuring a smoother user experience and well-structured data handling.

In the following sections, we will explore multiple approaches to tackle this challenge, from traditional for loops and filter methods to the more modern Set object and ES6 one-liners. By the end of this article, you will be equipped with the knowledge and expertise to remove duplicates from array JS using the most suitable method for your specific use case.

Whether you are a seasoned developer or a beginner looking to enhance your JavaScript skills, this article will serve as a comprehensive guide to help you master the art of removing duplicates from array JS, streamlining your code and making your applications more efficient and robust.

 

Different Methods to Remove Duplicates from an Array in JS

There are several methods you can use to remove duplicates from a JavaScript array. Here are some of the most commonly used ones:

 

1. Using Set and the Spread operator

One of the easiest and most efficient ways to remove duplicates from an array is to use a Set. A Set is an object in JavaScript that allows you to store unique values of any type, including primitive types like strings and numbers, as well as object references.

Here's an example of using a Set to remove duplicates from an array:

const arr = [1, 2, 2, 3, 4, 4, 5];
const uniqueArr = [...new Set(arr)];
console.log(uniqueArr);

Output

[ 1, 2, 3, 4, 5 ]

In this example, we create a new Set object from the original array, which automatically removes any duplicate values. We then use the spread operator (...) to convert the Set back to an array.

 

2. Using the filter() method

Another method for removing duplicates from an array is to use the filter method. The filter method creates a new array with all elements that pass the test implemented by the provided function.

Here's an example of using the filter method to remove duplicates from an array

const arr = [1, 2, 2, 3, 4, 4, 5];
const uniqueArr = arr.filter((value, index, self) => {
    return self.indexOf(value) === index;
});
console.log(uniqueArr);

Output

[ 1, 2, 3, 4, 5 ]

In this example, we use the filter method to create a new array that contains only the unique elements of the original array. The filter function checks whether the current value's index is equal to the first occurrence of the value in the array.

 

3. Using the reduce() method

You can also use the reduce() method to remove duplicates from an array. The reduce method executes a provided function for each value of the array, resulting in a single output value.

Here's an example of using the reduce method to remove duplicates from an array

const arr = [1, 2, 2, 3, 4, 4, 5];
const uniqueArr = arr.reduce((accumulator, currentValue) => {
    return accumulator.includes(currentValue)
        ? accumulator
        : [...accumulator, currentValue];
}, []);
console.log(uniqueArr);

Output

[ 1, 2, 3, 4, 5 ]

In this example, we use the reduce method to create a new array that contains only the unique elements of the original array. The reduce function checks whether the current value is already in the accumulator array, and if not, adds it to the accumulator.

 

4. Using indexOf() and splice() methods

You can also use the indexOf() and splice() methods to remove duplicates from an array. The indexOf() method returns the first index at which a given element can be found in the array, while the splice() method changes the contents of an array by removing or replacing existing elements.

Here's an example of using the indexOf() and splice() methods to remove duplicates from an array

let fruits = ["apple", "banana", "orange", "apple", "orange", "stawberry", "banana"];

for (let i = 0; i < fruits.length; i++) {
    if (fruits.indexOf(fruits[i]) !== i) {
        fruits.splice(i, 1);
        i--;
    }
}

console.log(fruits);

Output

[ 'apple', 'banana', 'orange', 'stawberry' ]

In this example, we first initialize an array fruits with some duplicate elements. We then iterate over the array using a for loop and check if the current element's index is the same as the first occurrence's index using the indexOf() method.

If the index is not the same, we know it's a duplicate and remove it using the splice() method. We also decrement the loop counter to avoid skipping an element.

Finally, we log the modified array fruits, which contains only the unique elements.

 

Removing Duplicates from Multidimensional Arrays

Removing duplicates from multidimensional arrays can be a bit more challenging compared to removing duplicates from single-dimensional arrays. In multidimensional arrays, elements can be arrays or objects themselves, requiring a more complex comparison approach. Here's a step-by-step guide on how to remove duplicates from a multidimensional array in JavaScript:

 

Using JSON.stringify and Set

Convert each sub-array or object to a JSON string, remove duplicates using Set, and convert back to the original structure.

const array = [[1, 2], [1, 2], [3, 4], [3, 4], [5, 6]];
const uniqueArray = [...new Set(array.map(JSON.stringify))].map(JSON.parse);
console.log(uniqueArray); // Output: [[1, 2], [3, 4], [5, 6]]

First, we use the map() method to convert each sub-array or object to a JSON string using JSON.stringify. Then, we create a new Set object with the JSON strings, effectively removing duplicates as Set only stores unique values. Finally, we use the spread operator and map() method to convert the JSON strings back to their original sub-array or object structure.

 

Using Array.prototype.filter Method for Arrays of Objects

Remove duplicates from an array of objects based on a specific property or a combination of properties.

const array = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
  { id: 1, name: "John" },
  { id: 3, name: "Doe" },
  { id: 2, name: "Jane" },
];

const uniqueArray = array.filter((value, index, self) => {
  return (
    index ===
    self.findIndex((item) => item.id === value.id && item.name === value.name)
  );
});

console.log(uniqueArray);
// Output: [{ id: 1, name: "John" }, { id: 2, name: "Jane" }, { id: 3, name: "Doe" }]

We use the filter() method to iterate through the array of objects and return a new array with objects that meet the specified condition. In this case, we use the findIndex() method to check if the current object's properties match any other object's properties in the array. If the current index is equal to the result of findIndex(), it means the object is unique and will be included in the resulting array.

 

Removing Duplicates Based on a Specific Property

When working with an array of objects, you might want to remove duplicates based on a specific property or key-value pair. In such cases, you can use the Array.prototype.filter method along with a custom condition to identify unique objects based on the desired property. Here's an example:

 

Using Array.prototype.filter Method for Objects with a Specific Property

Remove duplicates from an array of objects based on a specific property, while keeping the original order of the elements.

const array = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
  { id: 1, name: "Jack" },
  { id: 3, name: "Doe" },
  { id: 2, name: "Janet" },
];

const uniqueArray = array.filter((value, index, self) => {
  return index === self.findIndex((item) => item.id === value.id);
});

console.log(uniqueArray);
// Output: [{ id: 1, name: "John" }, { id: 2, name: "Jane" }, { id: 3, name: "Doe" }]

In this example, we use the filter() method to iterate through the array of objects and return a new array with objects that meet the specified condition. The condition checks if the current object's "id" property matches any other object's "id" property in the array. If the current index is equal to the result of findIndex(), it means the object with that specific property is unique and will be included in the resulting array. Note that the first occurrence of the duplicate object based on the specific property is retained in the resulting array.

 

Preserving the Original Array Order

Remove duplicates from an array while maintaining the original order of the elements using Array.prototype.filter.

const array = [3, 1, 4, 1, 2, 4, 5, 3];
const uniqueArray = array.filter((value, index, self) => {
  return self.indexOf(value) === index;
});
console.log(uniqueArray); // Output: [3, 1, 4, 2, 5]

The filter() method iterates through the array elements and returns a new array with elements that meet the specified condition. The condition checks if the current value's index in the array is equal to the current index. If true, it means the value is unique and will be included in the resulting array. This approach preserves the original order of elements in the array.

 

Removing Duplicates Using Lodash Library

Use the popular Lodash library's _.uniq() and _.uniqBy() functions to remove duplicates from arrays and arrays of objects, respectively.

const _ = require('lodash');

// Removing duplicates from a simple array
const array = [3, 1, 4, 1, 2, 4, 5, 3];
const uniqueArray = _.uniq(array);
console.log(uniqueArray); // Output: [3, 1, 4, 2, 5]

// Removing duplicates from an array of objects based on a specific property
const objArray = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
  { id: 1, name: "Jack" },
  { id: 3, name: "Doe" },
  { id: 2, name: "Janet" },
];

const uniqueObjArray = _.uniqBy(objArray, 'id');
console.log(uniqueObjArray);
// Output: [{ id: 1, name: "John" }, { id: 2, name: "Jane" }, { id: 3, name: "Doe" }]

First, we need to import the Lodash library by requiring it. To remove duplicates from a simple array, we use the _.uniq() function. For an array of objects, we use the _.uniqBy() function and provide the specific property to be used for identifying unique objects. Both functions maintain the original order of the elements in the array.

 

Best Practices for Removing Duplicates in JavaScript Arrays

Considering Performance

It's crucial to take the effectiveness of the various techniques into account while eliminating duplicates from an array. For huge arrays, certain methods may be slower than others or may take more time to process.

Given that it employs a specific set of values from the array, the Set technique is the quickest of all the ones that have been mentioned. Set, however, may not be the best option if the order of the items is crucial because it modifies the order.

The indexOf() and splice() methods, on the other hand, take longer for larger arrays and demand the use of a loop to run through the entire array.

Given that it only iterates through the array once, the reduce() technique is quicker than the filter() method. The filter() method, on the other hand, creates a new array that can affect performance for larger arrays.

 

Handling Edge Cases

When removing duplicates from an array, edge cases can happen. For instance, the methods outlined might not function as intended if the array contains objects.

The use of external libraries like Lodash, which offer more advanced techniques for addressing such scenarios, is advised in such circumstances.

It's also crucial to remember that the methods covered don't take the data types of the values into account because they only eliminate exact duplicates. There may be a need for further checks if data types are significant.

 

Summary

Removing duplicates from an array in JavaScript is a common task that can be accomplished using several methods, including Set, filter(), reduce(), and indexOf() with splice(). Each method has its advantages and disadvantages, and choosing the right method depends on the specific use case.

It's important to consider performance and edge cases when selecting a method for removing duplicates. Additionally, external libraries such as Lodash can be used for more complex use cases.

By understanding these methods and their best practices, developers can efficiently remove duplicates from arrays and improve the performance and readability of their code.

 

References

lodash - npm (npmjs.com)
Array.prototype.indexOf() - JavaScript | MDN (mozilla.org)
Array.prototype.splice() - JavaScript | MDN (mozilla.org)
Set - JavaScript | MDN (mozilla.org)
Array.prototype.filter() - JavaScript | MDN (mozilla.org)
Array.prototype.reduce() - JavaScript | MDN (mozilla.org)

 

Olorunfemi Akinlua

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment