Created
August 17, 2023 15:04
-
-
Save suhailgupta03/d4e9710bcddc8f2d9cc3a0d54a4ae0a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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);