Vanilla javascript for duplicates not working

Like @ppc said there is benefit in understanding both, but don’t postpone a big part of a language just because you don’t understand it, because that is a recipe for 1- overcomplicating things, 2- not getting the benefit of learning how to use more advanced features that are extremely important in the job market and 3- writing code that is hard to read and hard to debug. You can write dozens of for loops and if/else statements, but at some point you have to do something that makes you a little uncomfortable so you can take the next step. Consider the same idea but with a .filter() and an .indexOf() (don’t read if you haven’t tried writing it yourself):

test.filter(function(val, index){
  return index === test.indexOf(val);
});

or with an arrow function:

test.filter((val, index) => test.indexOf(val) === index);

It is so much cleaner and easier to read than using the loops, that I would consider the for loops a code smell in this case.

1 Like

Set example

var input = [1, 1, 2, 2, 3, 4]
var set = new Set(input)
var output = Array.from(set)

For…Of example (useful when you don’t need to care about the index)

var input = [1, 1, 2, 2, 3, 4]
var output = []
for (var n of input) {
  if (!output.includes(n)) {
   output.push(n)
  }
}

Regex example (unconventional - for amusement only)

var arr = [1, 1, 3, 3, 3, 4, 5,]
arr.sort((a, b) => a - b)
  .join('')
  .replace(/(\d)\1+/g, '$1')
  .split('')
  .map(Number)
2 Likes

lol @ the regex example. Obviously it would only work for sorted arrays with one digit numbers, but it is cool thinking outside of the box. I usually use Set in these cases, but in this case I think @jinisner would learn most from learning about .filter

2 Likes

@forkerino @0x0936 - i will have a look at them after i try myself , thanks

The array doesn’t need to be sorted, but you’re right that it only works for single digit numbers. I’ll call that scope creep - beyond the requirements of the project :slight_smile:

With sorted I meant that all the same numbers should be next to each other. It wouldn’t work with something like [1,2,1,2,1,2,3]

That’s true. Thanks for pointing that out - spoiler code fixed.

1 Like

@forkerino - hi i tried filter and it worked too, thanks …Though its a bit confusing as indexOf(currval) stores index of first instance while index of current value, this is a super shortcut, would have never understood any thing by this only …

var test = [3,3,3,3,3,1,1,1,5,7,7,7,6,3,3,3,3,3,3,3,7,3,9,23,12,12,12,12,12];

var res = test.filter(function(currval,index,arr){
    return  arr.indexOf(currval) === index;
});

console.log(res);

This code will seem simple soon enough. Congrats on making it work with filter. I can understand why you’d say that it is hard to grasp at first, but if you know that the return value of .indexOf() is the first occurrence of a value in an array, it reads quite well in my opinion. At least better than nested for loops where the code itself says very little about what it is supposed to be doing (in which case naming your variables and functions properly is very important and often comments are necessary as well). With .filter any reasonably experienced developer will understand what is going on there as the name says it all.

Here is a very simple solution using set:

var test = [3,3,3,1,1,1,5]; 
var noDuplicates = Array.from(new Set(test));

EDIT: I just saw 0x0936 already posted this.

@dcenatiempo - Yes , unfortunately i have not yet reached Set and so i do not understand it, thanks for your feed back …

Well there is no better time to learn it than now! I only learned about it last week!
It’s pretty simple, actually, set is very similar to array, except there are only allowed 1 unique occurance of each item.
So basically, my code takes an array with duplicates, turns it into a set (removing duplicates) and then turns it back into an array.