Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
If the element repeats, remove all its instances from array in JavaScript
When working with arrays in JavaScript, sometimes you need to remove all instances of elements that appear more than once, keeping only elements that appear exactly once. This is different from typical deduplication where you keep one instance of each duplicate.
For example, if the input is:
const arr = [763, 55, 43, 22, 32, 43, 763, 43];
console.log("Original array:", arr);
Original array: [ 763, 55, 43, 22, 32, 43, 763, 43 ]
The output should be:
[ 55, 22, 32 ]
Notice that 763 and 43 are completely removed because they appear multiple times, while 55, 22, and 32 remain because they appear only once.
Understanding indexOf() and lastIndexOf()
Array.prototype.indexOf() returns the index of the first occurrence of an element, while Array.prototype.lastIndexOf() returns the index of the last occurrence. If an element appears only once, both methods return the same index.
Using filter() with indexOf() and lastIndexOf()
The most efficient approach uses the filter() method combined with indexOf() and lastIndexOf():
const arr = [763, 55, 43, 22, 32, 43, 763, 43];
const deleteDuplicate = (arr) => {
const output = arr.filter((item, index, array) => {
return array.indexOf(item) === array.lastIndexOf(item);
});
return output;
};
console.log("Original array:", arr);
console.log("After removing duplicates:", deleteDuplicate(arr));
Original array: [ 763, 55, 43, 22, 32, 43, 763, 43 ] After removing duplicates: [ 55, 22, 32 ]
How It Works
The filter condition array.indexOf(item) === array.lastIndexOf(item) works as follows:
- For unique elements:
indexOf()andlastIndexOf()return the same index - For duplicate elements:
indexOf()returns the first occurrence index,lastIndexOf()returns the last occurrence index - Only elements where both indices match pass the filter
Alternative Approach Using Map
Another method uses a Map to count occurrences first, then filter based on count:
const arr = [763, 55, 43, 22, 32, 43, 763, 43];
const removeAllDuplicates = (arr) => {
const countMap = new Map();
// Count occurrences
arr.forEach(item => {
countMap.set(item, (countMap.get(item) || 0) + 1);
});
// Keep only items with count === 1
return arr.filter(item => countMap.get(item) === 1);
};
console.log("Using Map approach:", removeAllDuplicates(arr));
Using Map approach: [ 55, 22, 32 ]
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| indexOf/lastIndexOf | O(n²) | O(1) | Simple |
| Map counting | O(n) | O(n) | More verbose |
Conclusion
The indexOf/lastIndexOf approach is simple and readable for smaller arrays, while the Map approach offers better performance for larger datasets. Both methods effectively remove all instances of duplicate elements.
