Removing Duplicate Elements from a JavaScript Array
This challenge focuses on a common programming task: removing duplicate elements from an array. Eliminating duplicates is essential for data cleaning, ensuring unique entries in datasets, and optimizing performance in various algorithms. Your task is to write a JavaScript function that efficiently removes all duplicate values from a given array, preserving the original order of the remaining elements.
Problem Description
You are required to implement a JavaScript function named removeDuplicates that takes a single argument: an array of any data type. The function should return a new array containing only the unique elements from the input array, maintaining the original order of appearance. The original array should not be modified.
Key Requirements:
- Uniqueness: The returned array must contain only unique elements.
- Order Preservation: The order of the elements in the returned array must be the same as their first appearance in the input array.
- New Array: The function must return a new array; it should not modify the original input array.
- Handles Various Data Types: The function should work correctly with arrays containing numbers, strings, booleans, or a mix of these types.
- Handles Mixed Types Correctly: Ensure that elements of different types are treated as distinct, even if they have the same string representation (e.g.,
1(number) and"1"(string) should be considered different).
Expected Behavior:
The function should iterate through the input array and add each element to the new array only if it hasn't been encountered before. A simple way to track this is to use a data structure (like a Set) to store the elements that have already been seen.
Edge Cases to Consider:
- Empty Array: If the input array is empty, the function should return an empty array.
- Array with All Duplicates: If the input array contains only duplicate elements, the function should return an array containing only the first occurrence of that element.
- Array with Mixed Data Types: The function should correctly handle arrays containing a mix of data types (numbers, strings, booleans, etc.).
- Array with Null and Undefined: Consider how to handle
nullandundefinedvalues. Should they be treated as unique or removed? (For this challenge, treatnullandundefinedas unique values).
Examples
Example 1:
Input: [1, 2, 2, 3, 4, 4, 5]
Output: [1, 2, 3, 4, 5]
Explanation: The input array contains duplicate numbers. The function returns a new array with only the unique numbers, preserving the original order.
Example 2:
Input: ["apple", "banana", "apple", "orange", "banana"]
Output: ["apple", "banana", "orange"]
Explanation: The input array contains duplicate strings. The function returns a new array with only the unique strings, preserving the original order.
Example 3:
Input: [1, "1", true, 1, false, "true"]
Output: [1, "1", true, false, "true"]
Explanation: The input array contains mixed data types and values that might appear similar as strings. The function correctly distinguishes between the number `1` and the string `"1"`.
Example 4:
Input: []
Output: []
Explanation: The input array is empty. The function returns an empty array.
Constraints
- The input array can contain any data type (numbers, strings, booleans, null, undefined, objects, etc.).
- The length of the input array can be up to 100,000 elements.
- The function should have a time complexity of O(n), where n is the length of the input array. While a nested loop solution might work, it will likely fail the performance constraints for larger arrays.
- The function must not modify the original input array.
Notes
Consider using a Set data structure in JavaScript to efficiently track the elements you've already encountered. Sets provide constant-time (O(1)) lookups for checking if an element already exists. This will help you achieve the desired O(n) time complexity. Remember to create and return a new array.