The last few weeks I have been learning Python. I'm following this roadmap: 30-days-of-python Series' Articles by @arindam Dawn. I recommend it ! I love it π€©
Anyway, when I was learning Python I saw that the SET framework existed, and I was fascinated.
But thanks to that I discovered that it also existed in Javascript, and I didn't know it!
With SET we can remove duplicate elements from an array in a very easy way.
JavaScript's built-in Set object is described as a collection of values, where each value may occur only once. A Set object is also iterable, making it easily convertible to an array using the spread (...) operator.
π Here are some examples of this:
const nums = [1, 2, 2, 3, 1, 2, 4, 5, 5, 6, 4, 2, 6];
[...new Set(nums)] // [1, 2, 3, 4, 5, 6]
const something = ['anto', 'anto', 'antonella', 'ant', 'ant', 2, 4, 5, 6,2,2];
[...new Set(something)] // [ 'anto', 'antonella', 'ant', 2, 4, 5, 6 ]
const emojisFruits = ['π','π', 'π ','π' ,'π ','π' ,'π','π','π ','π'];
[...new Set(emojisFruits)] // [ 'π', 'π ', 'π', 'π', 'π', 'π ' ]
Very useful!!!
Did you also know about this?
Top comments (9)
nice.
for higher order function lovers , we can do the same thing by
Like @lukeshiru said, this works but is much slower.
O(n^2)
vsO(n)
. It's fine for small arrays, but would be slow as the array gets bigger.numsObject = [{id: 1, name: 'one', {id: 2, name: 'two'}] ?
new Map(arr) is so mush faster than this
That's really simplest way to make a unique list.
Thanks for this tip! π― I saw it once, absolutely forgot about it. How useful! And it's freaking easy!βοΈ
Awesome!!
Thanks!!
nice, thx for the tip
I used in my school