DEV Community

Cover image for Removing Duplicates from JavaScript Arrays: Exploring Multiple Methods for Clean Data
Shaikh AJ
Shaikh AJ

Posted on

Removing Duplicates from JavaScript Arrays: Exploring Multiple Methods for Clean Data

Introduction:

Duplicates within JavaScript arrays can introduce data inconsistencies and hinder efficient data processing. Fortunately, JavaScript offers several methods to eliminate duplicates and ensure clean data. In this blog post, we will delve into various techniques, examining their intricacies and discussing their applicability to both simple arrays and arrays of objects. By the end, you'll have a comprehensive understanding of how to effectively remove duplicates from JavaScript arrays.

1. Using Set: Leveraging Unique Values
JavaScript Sets are built-in data structures that store only unique values. By converting an array to a Set and then back to an array, we can effortlessly eliminate duplicates. Let's explore this method with both simple arrays and arrays of objects:

Simple Array Example:

const array = [1, 2, 3, 2, 4, 1, 5];
const uniqueArray = Array.from(new Set(array));

console.log(uniqueArray); // [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Array of Objects Example:

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Charlie' }
];

const uniqueUsers = Array.from(new Set(users.map(JSON.stringify)), JSON.parse);

console.log(uniqueUsers);
/*
[
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
]
*/
Enter fullscreen mode Exit fullscreen mode

In both examples, the array is converted to a Set using the new Set() constructor. For arrays of objects, we utilize the map function to transform the objects into strings using JSON.stringify and then convert them back to objects using JSON.parse.

2. Using Filter and indexOf: Iterating for Uniqueness
Another approach involves iterating over the array and using the filter method along with the indexOf method to identify and remove duplicates. This method can be applied to both simple arrays and arrays of objects:

Simple Array Example:

const array = [1, 2, 3, 2, 4, 1, 5];
const uniqueArray = array.filter((value, index, self) => self.indexOf(value) === index);

console.log(uniqueArray); // [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Array of Objects Example:

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Charlie' }
];

const uniqueUsers = users.filter((user, index, self) => {
  const firstIndex = self.findIndex((u) => u.id === user.id);
  return index === firstIndex;
});

console.log(uniqueUsers);
/*
[
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
]
*/
Enter fullscreen mode Exit fullscreen mode

In both examples, the filter method is used to keep only the elements that have their first occurrence index equal to the current index. For arrays of objects, we utilize the findIndex method to identify the first occurrence based on a specific property (in this case, the id property).

3. Using Reduce: Aggregating Unique Values
The reduce method provides

a powerful way to remove duplicates by accumulating unique values into a new array. Let's explore this method with both simple arrays and arrays of objects:

Simple Array Example:

const array = [1, 2, 3, 2, 4, 1, 5];
const uniqueArray = array.reduce((accumulator, value) => {
  if (!accumulator.includes(value)) {
    accumulator.push(value);
  }
  return accumulator;
}, []);

console.log(uniqueArray); // [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Array of Objects Example:

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Charlie' }
];

const uniqueUsers = users.reduce((accumulator, user) => {
  const existingUser = accumulator.find((u) => u.id === user.id);
  if (!existingUser) {
    accumulator.push(user);
  }
  return accumulator;
}, []);

console.log(uniqueUsers);
/*
[
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
]
*/
Enter fullscreen mode Exit fullscreen mode

In both examples, the reduce method accumulates unique values into the accumulator array by checking if the value or object already exists in it. For arrays of objects, the find method is used to identify existing objects based on a specific property.

4. Using Map: Tracking Unique Values
The map method can also be utilized to remove duplicates by tracking unique values within an array:

Simple Array Example:

const array = [1, 2, 3, 2, 4, 1, 5];
const uniqueArray = Array.from(new Map(array.map((value) => [value, value])).values());

console.log(uniqueArray); // [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Array of Objects Example:

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Charlie' }
];

const uniqueUsers = Array.from(new Map(users.map((user) => [user.id, user])).values());

console.log(uniqueUsers);
/*
[
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
]
*/
Enter fullscreen mode Exit fullscreen mode

In both examples, the map method is used to create a new map with keys representing the unique values. The resulting map is then converted back to an array using Array.from and extracting the values.

5. Using forEach and includes: Iterating and Checking
This method involves iterating over the array using the forEach method and checking if each element is already present in a new array using the includes method.

Simple Array Example:

const array = [1, 2, 3, 2, 4, 1, 5];
const uniqueArray = [];

array.forEach((value) => {
  if (!uniqueArray.includes(value)) {
    uniqueArray.push(value);
  }
});

console.log(uniqueArray); // [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Array of Objects Example:

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Charlie' }
];

const uniqueUsers = [];

users.forEach((user) => {
  const exists = uniqueUsers.some((u) => u.id === user.id);
  if (!exists) {
    uniqueUsers.push(user);
  }
});

console.log(uniqueUsers);
/*
[
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
]
*/
Enter fullscreen mode Exit fullscreen mode

In both examples, the forEach method iterates over the array, and for each element, it checks if the unique array already includes that value or object. If not, it is added to the unique array.

6. Using a Temporary Object: Tracking Unique Values
This method involves creating a temporary object to keep track of unique values. This approach works well for simple arrays with primitive values.

Simple Array Example:

const array = [1, 2, 3, 2, 4, 1, 5];
const uniqueArray = [];

const tempObj = {};

array.forEach((value) => {
  if (!tempObj[value]) {
    tempObj[value] = true;
    uniqueArray.push(value);
  }
});

console.log(uniqueArray); // [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Array of Objects Example:

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Charlie' }
];

const uniqueUsers = [];
const tempObj = {};

users.forEach((user) => {
  if (!tempObj[user.id]) {
    tempObj[user.id] = true;
    uniqueUsers.push(user);
  }
});

console.log(uniqueUsers);
/*
[
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
]
*/
Enter fullscreen mode Exit fullscreen mode

In both examples, a temporary object is used to keep track of unique values by using their keys. If the key does not exist in the temporary object, the value or object is added to the unique array, and the key is set to true in the temporary object.

7. Using ES6 Spread Operator and Set: Modern Approach
This method combines the use of the spread operator and Set to create a concise solution for removing duplicates.

Simple Array Example:

const array = [1, 2, 3, 2, 4, 1, 5

];
const uniqueArray = [...new Set(array)];

console.log(uniqueArray); // [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Array of Objects Example:

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Charlie' }
];

const uniqueUsers = [...new Set(users.map(JSON.stringify))].map(JSON.parse);

console.log(uniqueUsers);
/*
[
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
]
*/
Enter fullscreen mode Exit fullscreen mode

In both examples, the spread operator ... is used to create a new array by spreading the unique values obtained from the Set. For arrays of objects, the map function is used to transform the objects into strings using JSON.stringify before applying the Set operation. Finally, the objects are transformed back from strings to objects using JSON.parse.

Conclusion:
Removing duplicates from JavaScript arrays is crucial for maintaining clean data and optimizing data processing. By exploring the various methods discussed in this post, including the use of Set, filter and indexOf, reduce, and map, you now have a comprehensive toolkit to handle duplicate elimination. Apply the appropriate method based on your array's structure and complexity, ensuring the integrity and efficiency of your data manipulation in JavaScript.

Top comments (0)