1. Problem Description
This is a problem to create and return a set of unique elements from the given array. This type of problem is frequently asked in coding tests using JavaScript and helps in understanding the concept of sets.
2. Problem Definition
**Problem:** Write a function uniqueElements(arr)
that takes an array as input and returns a new array with duplicates removed.
// Example input
uniqueElements([1, 2, 2, 3, 4, 4, 5]); // Returns: [1, 2, 3, 4, 5]
uniqueElements(['apple', 'banana', 'apple']); // Returns: ['apple', 'banana']
3. Approach
There are various ways to solve this problem. However, utilizing the properties of a set in JavaScript is the most efficient way. The set data structure automatically removes duplicates, as it only stores unique values.
3.1. Method 1: Using the Set Object
Using the Set object to remove duplicates is very intuitive. When you pass an array to the Set, you get a Set object with duplicates removed, which can then be converted back to an array to return the result.
function uniqueElements(arr) {
return [...new Set(arr)];
}
3.2. Method 2: Using Filter and Index
Another method is to use the array’s filter
method to remove duplicates. This approach can determine whether an item is a duplicate by comparing its first occurrence index with the current index.
function uniqueElements(arr) {
return arr.filter((item, index) => arr.indexOf(item) === index);
}
3.3. Method 3: Removing Duplicates Using an Object
You can use an object in a performance-efficient way to remove duplicates. Store each element as a key in the object, and at the end, create a new array using only the keys of this object.
function uniqueElements(arr) {
const uniqueObj = {};
arr.forEach(item => {
uniqueObj[item] = true;
});
return Object.keys(uniqueObj);
}
4. Code Implementation
Below is an implementation example using the first method, which employs Set as described above.
function uniqueElements(arr) {
return [...new Set(arr)];
}
// Tests
console.log(uniqueElements([1, 2, 2, 3, 4, 4, 5])); // [1, 2, 3, 4, 5]
console.log(uniqueElements(['apple', 'banana', 'apple'])); // ['apple', 'banana']
5. Time Complexity Analysis
The time complexity of all methods is O(n), where n is the length of the array. The method using Set is the most concise and intuitive, and since it’s a feature provided by ES6, it also enhances code readability. The methods using filter or objects may have additional memory usage.
6. Conclusion
Through this problem, we have understood the concept of sets in JavaScript and learned various ways to remove duplicates from an array. Such problems are commonly featured in coding tests, so it’s essential to familiarize yourself with the data structures and algorithms that can be utilized. Understanding and using new features like Set in ES6 helps in writing cleaner code.
7. Additional Practice Problems
There may be other problems such as:
- Write a function to count the number of duplicate elements in an array
- Find common elements between two arrays
- Count non-duplicate characters in a string
I hope solving these problems helps you learn various features of JavaScript.