## Find All Duplicates in an Array in JavaScript

Finding duplicates in an array is a common task in JavaScript programming. This article will guide you through several efficient methods to achieve this goal.

**Understanding the Problem**

Given an array of integers, we need to find all the elements that appear more than once. Let's consider this example:

```
const nums = [4, 3, 2, 7, 8, 2, 3, 1];
```

In this array, the numbers `2`

and `3`

are duplicates.

**Methods to Find Duplicates**

Here are some effective methods to find all duplicates in a JavaScript array:

**1. Using a Set**

Sets in JavaScript are collections of unique values. We can leverage this property to find duplicates. Here's how:

```
function findDuplicates(nums) {
const seen = new Set();
const duplicates = [];
for (let i = 0; i < nums.length; i++) {
if (seen.has(nums[i])) {
duplicates.push(nums[i]);
} else {
seen.add(nums[i]);
}
}
return duplicates;
}
const nums = [4, 3, 2, 7, 8, 2, 3, 1];
const duplicates = findDuplicates(nums);
console.log(duplicates); // Output: [2, 3]
```

**Explanation:**

- We create a
`Set`

called`seen`

to store unique elements encountered so far. - We iterate through the array.
- If an element is already in
`seen`

, we know it's a duplicate and add it to the`duplicates`

array. - If the element is not in
`seen`

, we add it to the`seen`

Set. - Finally, we return the
`duplicates`

array.

**2. Using Object Keys**

We can utilize the keys of an object to track the frequency of each element in the array.

```
function findDuplicates2(nums) {
const count = {};
const duplicates = [];
for (let i = 0; i < nums.length; i++) {
const num = nums[i];
if (count[num]) {
count[num]++;
if (count[num] === 2) {
duplicates.push(num);
}
} else {
count[num] = 1;
}
}
return duplicates;
}
const nums = [4, 3, 2, 7, 8, 2, 3, 1];
const duplicates = findDuplicates2(nums);
console.log(duplicates); // Output: [2, 3]
```

**Explanation:**

- We create an empty object
`count`

to store the frequency of each element. - We iterate through the array.
- If an element is already a key in
`count`

, we increment its count. If the count becomes 2, it means we found a duplicate. - If the element is not a key in
`count`

, we add it as a key and set its value to 1. - We return the
`duplicates`

array.

**3. Using Sorting**

We can sort the array and then find duplicates by comparing adjacent elements.

```
function findDuplicates3(nums) {
nums.sort((a, b) => a - b);
const duplicates = [];
for (let i = 1; i < nums.length; i++) {
if (nums[i] === nums[i - 1]) {
duplicates.push(nums[i]);
}
}
return duplicates;
}
const nums = [4, 3, 2, 7, 8, 2, 3, 1];
const duplicates = findDuplicates3(nums);
console.log(duplicates); // Output: [2, 3]
```

**Explanation:**

- We sort the array using the
`sort()`

method. - We iterate through the array, comparing each element with its preceding element.
- If they are equal, it means we found a duplicate and add it to the
`duplicates`

array. - We return the
`duplicates`

array.

**Choosing the Best Method**

The best method depends on the size of your array and the specific requirements of your application.

**Using a Set**is generally the most efficient for large arrays as it provides constant-time lookups.**Using Object Keys**can be more efficient than sorting for arrays with a limited range of values.**Using Sorting**is a simple and intuitive approach, but it may not be the most efficient for large arrays.

Remember to choose the method that best balances efficiency and readability for your specific use case.