Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 29, 2021 04:21 pm GMT

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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To