An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
August 29, 2021 04:21 pm GMT
Original Link: https://dev.to/frontendengineer/code-this-4-remove-duplicates-13m5
Code This 4: Remove Duplicates
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.
Code: https://codepen.io/angelo_jin/pen/PojPRzQ
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!
Original Link: https://dev.to/frontendengineer/code-this-4-remove-duplicates-13m5
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To