r/learnjavascript • • May 14 '21

Remove Duplicate Value from array

Post image
234 Upvotes

89 comments sorted by

View all comments

Show parent comments

7

u/space_scarab May 14 '21
arr.filter((el, index) => {
    return arr.indexOf(el) === index;
});

11

u/47milliondollars May 14 '21

Oh holy shit, never thought about this. Looks like the indexOf is finding the first match, and so the filter drops anything of the same value that isn't at the first detected index? Pretty neat.

10

u/[deleted] May 14 '21 edited May 14 '21
arr.filter((el, index) => arr.indexOf(el) === index);

Works too!

(but worth saying that shorthand isn't always the most readable and shouldn't be your default approach!)

3

u/[deleted] May 15 '21

Honestly that shorthand is way easier to parse - for me at least

2

u/StoneCypher May 14 '21

is there any difference there other than switching to an arrow?

0

u/[deleted] May 14 '21

[deleted]

-5

u/StoneCypher May 14 '21

Thanks, I didn't ask what an arrow was.

What I asked was "is that the only change you made"

Notably, you shouldn't have that "auto-corrected," because arrows and standard functions differ in how they bind this. That should be hand-corrected. In many cases in practice, this will damage transitional-style classes.

7

u/47milliondollars May 14 '21

Dude, way to be a jerk to someone who kindly took the time to respond to that question (why would they think you were asking if they made any other changes when you're looking right at the 1-3 lines of code?). Also, their response was perfectly accurate for this code example we are looking at, the context of "this" is totally irrelevant here.

-3

u/StoneCypher May 14 '21

(why would they think you were asking if they made any other changes when you're looking right at the 1-3 lines of code?).

The point of the original question was to ask why this person is holding up a non-change as a change. I was trying to make sure I wasn't overlooking something else more substantive.

 

Also, their response was perfectly accurate for this code example we are looking at, the context of "this" is totally irrelevant here.

Yes, I'm ... kind of relying on that.

Literally the only difference between the two pieces of code is something that is totally irrelevant here.

It's like taking out the newlines and "proposing it as an alternative." Literally.

4

u/amoliski May 14 '21

Doesn't hurt to show both forms so if someone isn't familiar with them they can see they are similar.

2

u/StoneCypher May 14 '21

Doesn't hurt to show both forms so if someone isn't familiar with them they can see they are similar.

Then ask the guy who proposed them

I love how you guys are still downvoting me after he got caught lying about having made benchmarks

1

u/grooomps May 15 '21

So a function without a fat arrow doesn't have explicit return?

1

u/space_scarab May 14 '21

ah, yes, cleaner. thanks!

3

u/learning_account_01 May 14 '21

Man. I wish i can think like this. How do you do ? I was stuck at this smilar knida problem (find if array contains duplicate value or not and just return true or false.) and it never clicked me.

4

u/StoneCypher May 14 '21

Man. I wish i can think like this. How do you do

it's the accepted answer from the first result on googling "javascript unique"

https://stackoverflow.com/a/14438954/763127

it's pretty standard in the javascript community

4

u/learning_account_01 May 14 '21

Oh that shows how noob I am. I have lot to learn indeed. Thanks.

3

u/amoliski May 14 '21

Even without SO giving you that answer, it just takes practice and repetition to figure these things out on your own.

If you use indexOf enough times, you eventually learn (and reinforce in your brain) that indexOf returns the first match.

If you use filter enough, the mechanics become second nature.

Once you have both ideas cemented through practice, the connections just start to happen for you.

2

u/47milliondollars May 14 '21

Don't worry fren, a huge part of coding is googling stuff, you're not expected to think up this shit on your own :)

2

u/learning_account_01 May 14 '21

you're not expected to think up this shit on your own

Well thank god otherwise I can't even dare to dream of becoming a programmer some day.

1

u/Parkreiner May 15 '21

One comment and a question.

First, this approach will not work for non-primitive values. If you're dealing with an array of arrays or an array of objects or even an array of functions, it'll just compare their references. So if the array has two objects that have the same contents but were declared separately, you'll still get values that look like duplicates. That might be just fine for some applications, but if it's not, you'll have to do a deeper comparison.

Second, is this actually better? This isn't just rhetorical; I'm actually asking. Filtering is O(n), but so is indexOf, so since the indexOf call is inside the filter, the whole operation is O(n2). Meanwhile, generating a set is also O(n), as is destructing it into an array. But since they're done back-to-back and not inside each other, it's O(2n). I know that the highest factor doesn't always tell the whole story, but are there times when the set approach would perform better?