Sets bhi array ki tarah kaam karta hai or ye bhi iterable hota hai.
Array me duplicate value le sakte hain but set duplicate value ko ignore karta hai
sets ka apna method hota hai
new Sets()
no index base access
order is not guaranteed
const numbers = new Sets([1,2,3,4]);
agar hume kuck isme add karna ho to-
numbers.add(2)
numbers.add(3)
agar hume check karna hai koi element hai ya nahin
numbers.has(1) // return true or false
const items = ['item1', 'item2', 'item3'];
const numbers = new Set();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(6);
numbers.add(items);
if(numbers.has(1)){
console.log("1 is present")
}else{
console.log("1 is not present")
}
for(let number of numbers){
console.log(number);
}
const myArray = [1,2,4,4,5,6,5,6];
const uniqueElements = new Set(myArray);
let length = 0;
for(let element of uniqueElements){
length++;
}
console.log(length);