Skip to content

Instantly share code, notes, and snippets.

@suhailgupta03
Created August 17, 2023 15:04
Show Gist options
  • Save suhailgupta03/d4e9710bcddc8f2d9cc3a0d54a4ae0a4 to your computer and use it in GitHub Desktop.
Save suhailgupta03/d4e9710bcddc8f2d9cc3a0d54a4ae0a4 to your computer and use it in GitHub Desktop.
let studentAgeList = [
20, 21, 22, 20, 23, 25, 27, 22, 29, 30
];
console.log(studentAgeList)
// unique ages = [20, 21, 22, 23, 25, 27, 29, 30]
var ageSet = new Set();
// we use set when we want to store
// unique values and we don't care about
// the order
ageSet.add(20);
ageSet.add(21);
ageSet.add(22);
ageSet.add(20);
ageSet.add(20);
ageSet.add(20);
ageSet.add(20);
ageSet.add(25);
console.log(ageSet); // Set { 20, 21, 22 }
console.log(ageSet.has(20));
console.log(ageSet.size);
@suhailgupta03
Copy link
Author

let listWithDuplicateValues = [1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 10];
let uniqueValues = new Set(listWithDuplicateValues);
console.log(uniqueValues);

console.log(listWithDuplicateValues.length, "..", uniqueValues.size);

@suhailgupta03
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment