Remove Duplicates from Array
Given an array, your task is to return a new array with all duplicate elements removed.
You should preserve the order of the first occurrence of each element.
Input
- An array of any primitive values:
number,string,boolean,null, orundefined.
Output
- A new array containing only unique elements, in the order they first appear.
Edge Cases
-
Empty array should return an empty array.
-
Duplicates can be of different types (e.g., 1 and "1" are not the same).
-
Object and array references are considered unique even if they look identical.
Examples
removeDuplicates([1, 2, 2, 3, 4, 4]); // Output: [1, 2, 3, 4] removeDuplicates(["a", "b", "a", "c"]); // Output: ["a", "b", "c"] removeDuplicates([1, "1", 1]); // Output: [1, "1"] removeDuplicates([]); // Output: [] removeDuplicates([true, false, true]); // Output: [true, false]
Companies:
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
