Answer the question
In order to leave comments, you need to log in
How does the distinct counterpart work?
Here
https://stackblitz.com/edit/angular-2ic5vr
reproduced an example from a book who would like to understand how it works in detail.
I'm only interested in one line in the product.repository.ts file:
.filter((c, index, array) => array.indexOf(c) == index);
Answer the question
In order to leave comments, you need to log in
First of all, I don't understand why there is a comparison with index.
true
exactly once - since it indexOf
returns the first index by which you can find an element equal to the specified one. That is, if indexOf
the current element is equal to the current index, then the element is encountered for the first time, and it should be left.
I don’t know what kind of book you have there that offers examples of algorithms with quadratic complexity, but for real projects it’s better not to write like that. do something through sets:Array.from(new Set(sourceArray]))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question