DEV Community

Omor Faruk
Omor Faruk

Posted on

Handling Duplicates in JavaScript Arrays: Techniques and Best Practices

Image description

In JavaScript development, managing data efficiently is crucial. One common challenge developers face is handling duplicate values in arrays. This article will explore different methods to identify and eliminate duplicates, with a focus on both simple arrays and arrays of objects.

Understanding Duplicates in Arrays

When working with arrays, duplicates can lead to incorrect results, inefficient processing, or unexpected behavior in applications. Therefore, it’s essential to implement a robust strategy to filter out duplicates effectively.

1. Removing Duplicates from Simple Arrays

Let’s begin with a straightforward example. Suppose you have an array of numbers that contains duplicates:

let numberArray = [1, 2, 3, 3, 4, 5, 6, 5, 7, 10, 9, 9];
let uniqueNumbers = [];

for (let i = 0; i < numberArray.length; i++) {
  let isDuplicate = false;

  for (let j = 0; j < uniqueNumbers.length; j++) {
    if (numberArray[i] === uniqueNumbers[j]) {
      isDuplicate = true;
      break;
    }
  }

  if (!isDuplicate) {
    uniqueNumbers.push(numberArray[i]);
  }
}

console.log(uniqueNumbers); // Output: [1, 2, 3, 4, 5, 6, 7, 10, 9]
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Outer Loop: Iterates through each element in the original array (numberArray).
  • Inner Loop: Checks if the current element already exists in the uniqueNumbers array. If it does, it sets the isDuplicate flag to true and breaks out of the inner loop.
  • Condition: If the element is not a duplicate, it gets added to the uniqueNumbers.

While this approach works, it is not optimal for larger datasets due to its O(n²) time complexity, which can slow down performance.

2. Handling Duplicates in Arrays of Objects

When dealing with arrays of objects, you may want to remove duplicates based on specific properties, such as an id field. Below is an example illustrating how to achieve this:

let userArray = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Jane' },
  { id: 3, name: 'Bob' },
  { id: 3, name: 'Bob' }, // Duplicate
  { id: 4, name: 'Alice' },
  { id: 5, name: 'Eve' },
  { id: 5, name: 'Eve' }, // Duplicate
  { id: 6, name: 'Charlie' },
  { id: 7, name: 'David' },
  { id: 10, name: 'Edward' },
  { id: 9, name: 'Frank' },
  { id: 9, name: 'Frank' } // Duplicate
];

let uniqueUsers = [];

for (let i = 0; i < userArray.length; i++) {
  let isDuplicate = false;

  // Compare based on the 'id' property
  for (let j = 0; j < uniqueUsers.length; j++) {
    if (userArray[i].id === uniqueUsers[j].id) {
      isDuplicate = true;
      break;
    }
  }

  // If it's not a duplicate, add the object to the unique array
  if (!isDuplicate) {
    uniqueUsers.push(userArray[i]);
  }
}

console.log(uniqueUsers);
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • This code follows a similar logic to the previous example, but it checks for duplicates based on the id property of the objects in the array.

3. Best Approach for Removing Duplicates

For larger datasets, a more efficient approach is to use a map or object to keep track of seen identifiers. Here’s a refined example:

let userArray = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Jane' },
  { id: 3, name: 'Bob' },
  { id: 3, name: 'Bob' }, // Duplicate
  { id: 4, name: 'Alice' },
  { id: 5, name: 'Eve' },
  { id: 5, name: 'Eve' }, // Duplicate
  { id: 6, name: 'Charlie' },
  { id: 7, name: 'David' },
  { id: 10, name: 'Edward' },
  { id: 9, name: 'Frank' },
  { id: 9, name: 'Frank' } // Duplicate
];

let uniqueUsers = [];
let seenIds = {}; // This will track unique IDs

for (let i = 0; i < userArray.length; i++) {
  let currentId = userArray[i].id;

  // If the id hasn't been seen before, add the object to the unique array
  if (!seenIds[currentId]) {
    seenIds[currentId] = true; // Mark the id as seen
    uniqueUsers.push(userArray[i]);  // Add the object to the unique array
  }
}

console.log(uniqueUsers);
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • seenIds: An object that tracks which IDs have been encountered.
  • Efficiency: This method has a time complexity of O(n), making it more suitable for large datasets as it reduces the number of comparisons needed.

Conclusion

Handling duplicates in arrays is a vital skill for any JavaScript developer. By employing the methods discussed in this article—ranging from basic iterations to optimal solutions using maps or objects—you can efficiently manage data and ensure your applications run smoothly.

By understanding the structure of your data and choosing the right technique, you can enhance performance and maintainability in your projects. The optimal approach, in particular, allows for scalability, which is crucial as your datasets grow.

Feel free to adapt these examples to suit your application’s needs and keep your codebase clean and efficient!

Top comments (0)