sets with arrays javascript code example
Example 1: array to set javascript
const myArray = [1,2,3,1,5,8,1,2,9,4];
const unique = [...new Set(myArray)];
const myString = ["a","b","c","a","d","b"];
const uniqueString = [...new Set(myString)];
Example 2: hashset in javascript
let mySet = new Set()
mySet.add(1)
mySet.add(5)
mySet.add(5)
mySet.add('some text')
let o = {a: 1, b: 2}
mySet.add(o)
mySet.add({a: 1, b: 2})
mySet.has(1)
mySet.has(3)
mySet.has(5)
mySet.has(Math.sqrt(25))
mySet.has('Some Text'.toLowerCase())
mySet.has(o)
mySet.size
mySet.delete(5)
mySet.has(5)
mySet.size
console.log(mySet)