This content originally appeared on DEV Community and was authored by Let's Code
Interview Question #4:
Write a function that will remove duplicate in an array.?❓ You can get a variation of this question as Get unique characters from a list.
If you need practice, try to solve this on your own. I have included 2 potential solutions below.
Note: There are many other potential solutions to this problem.
Feel free to bookmark ? even if you don't need this for now. You may need to refresh/review down the road when it is time for you to look for a new role.
Solution #1: ES6 Set
function removeDuplicates(array) {
return [...new Set(array)]
}
Solution #2: Object
function removeDuplicates(array) {
const map = {}
for (const char of array) {
if (map[char]) {
map[char]++
} else {
map[char] = 1
}
}
return Object.keys(map)
}
In case you like a video instead of bunch of code ??
Happy coding and good luck if you are interviewing!
This content originally appeared on DEV Community and was authored by Let's Code
![](https://www.radiofree.org/wp-content/plugins/print-app/icon.jpg)
Let's Code | Sciencx (2021-08-29T16:21:06+00:00) Code This #4: Remove Duplicates. Retrieved from https://www.scien.cx/2021/08/29/code-this-4-remove-duplicates/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.