Embed presentation
Download to read offline
![const myArr = [1, 2, 3];
console.log(myArr[0]);
// Output: 1
const mySet
= new Set([1, 2, 3]);
console.log(mySet[0]);
// undefined
const arr1 = [1,2,3,3];
// Initializes an array with
4 items
const set1 = new Set(
[1, 2, 3, 3]
); // Initializes with 3
items
// method 1
var arr1 = [ ];
// method 2
var arr2 = new array();
// Initializes an empty set
const set1 = new Set();
// Initialize with value
const set2 = new Set([1, 2,
3]);
Initialize a set
Duplicate values are not allowed
Items can be accessed using index Items cannot be accessed using index
Initialize an array
Duplicate values are allowed
VSARRAY SET
[🍎, 🍎, 🍐, 🍐, 🍊] [🍎,🍎, 🍐, 🍐, 🍊] [🍎, 🍎, 🍐, 🍐, 🍊] [🍎, 🍐, 🍊]](https://image.slidesharecdn.com/kznldpflq0qik2uxfeeh-signature-99b787c0adaa9b6dacfe8a833739dc17bd7c9e378feb8e9f8c09821d8c2792c7-poli-200609035842/75/Array-vs-set-in-JavaScript-1-2048.jpg)
![By combining features of array and a set in
javascript, we get more powerful code.
Example
Easy shortcut to remove duplicates from an array
Brought to you by Ideas2IT
TRICKS TO REMOVE
DUPLICATES FROM ARRAY
// array with duplicates
let array1 = [1,1,2,2,3,3,4,4];
// create a Set with array
let array2 = Array.from(new Set(array1));
console.log(array2); // [ 1, 2, 3, 4 ]](https://image.slidesharecdn.com/kznldpflq0qik2uxfeeh-signature-99b787c0adaa9b6dacfe8a833739dc17bd7c9e378feb8e9f8c09821d8c2792c7-poli-200609035842/75/Array-vs-set-in-JavaScript-2-2048.jpg)

This document compares and contrasts arrays and sets in JavaScript. It notes that sets do not allow duplicate values, while arrays do. Items in an array can be accessed using indexes, but items in a set cannot. The document also provides an example of using a set to remove duplicate values from an array in one line of code.
![const myArr = [1, 2, 3];
console.log(myArr[0]);
// Output: 1
const mySet
= new Set([1, 2, 3]);
console.log(mySet[0]);
// undefined
const arr1 = [1,2,3,3];
// Initializes an array with
4 items
const set1 = new Set(
[1, 2, 3, 3]
); // Initializes with 3
items
// method 1
var arr1 = [ ];
// method 2
var arr2 = new array();
// Initializes an empty set
const set1 = new Set();
// Initialize with value
const set2 = new Set([1, 2,
3]);
Initialize a set
Duplicate values are not allowed
Items can be accessed using index Items cannot be accessed using index
Initialize an array
Duplicate values are allowed
VSARRAY SET
[🍎, 🍎, 🍐, 🍐, 🍊] [🍎,🍎, 🍐, 🍐, 🍊] [🍎, 🍎, 🍐, 🍐, 🍊] [🍎, 🍐, 🍊]](https://image.slidesharecdn.com/kznldpflq0qik2uxfeeh-signature-99b787c0adaa9b6dacfe8a833739dc17bd7c9e378feb8e9f8c09821d8c2792c7-poli-200609035842/75/Array-vs-set-in-JavaScript-1-2048.jpg)
![By combining features of array and a set in
javascript, we get more powerful code.
Example
Easy shortcut to remove duplicates from an array
Brought to you by Ideas2IT
TRICKS TO REMOVE
DUPLICATES FROM ARRAY
// array with duplicates
let array1 = [1,1,2,2,3,3,4,4];
// create a Set with array
let array2 = Array.from(new Set(array1));
console.log(array2); // [ 1, 2, 3, 4 ]](https://image.slidesharecdn.com/kznldpflq0qik2uxfeeh-signature-99b787c0adaa9b6dacfe8a833739dc17bd7c9e378feb8e9f8c09821d8c2792c7-poli-200609035842/75/Array-vs-set-in-JavaScript-2-2048.jpg)