What is the difference between using Set() and array? Does this do the same or it depends on what you want to use them for? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the difference between using Set() and array? Does this do the same or it depends on what you want to use them for?

I'm asking because I'm seeing a little bit of similarities. #Newbie For example: ```let arr = [] arr.push(1,2,3,4,5)``` And ... ```let arr = new Set() arr.add(1).add(2).add(3).add(4).add(5) for (let i of arr.values()) { console.log(i) }```

3rd Jul 2019, 7:33 PM
Mphumeleli Ntetha
Mphumeleli Ntetha - avatar
2 Answers
0
Besides the interface wich differ from one to another, the main difference is that you can store multiple times a v8alue in an Array, while a Set store uniques values... For example: var v, a = [], s = new Set(), t =''; a.push(1,2,3,2,5); s.add(1).add(2).add(3).add(2).add(4); for (v of a) t += (t ? ',' : '')+v; console.log('a:',t); t = ''; for (v of s.values()) t += (t ? ',' : '')+v; console.log('s:',t);
26th Mar 2020, 4:02 PM
visph
visph - avatar
- 1
In map, you can refer to a value using the keys and you can perform actions on it using keys.
29th Jun 2020, 5:58 AM
Aswin Manoj
Aswin Manoj - avatar