Last active
July 27, 2021 23:38
-
-
Save yitonghe00/fae0a8adb4c1688523af12809ed734ff to your computer and use it in GitHub Desktop.
This file contains 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
class Group { | |
// Your code here. | |
constructor() { | |
this.values = []; | |
} | |
add(elem) { | |
if (!this.has(elem)) { | |
this.values.push(elem); | |
} | |
} | |
delete(elem) { | |
const idx = this.values.indexOf(elem); | |
if (idx >= 0) { | |
this.values.splice(idx, 1); | |
} | |
} | |
has(elem) { | |
return this.values.indexOf(elem) >= 0; | |
} | |
static from(itr) { | |
const g = new Group(); | |
for (const elem of itr) { | |
g.add(elem); | |
} | |
return g; | |
} | |
} | |
let group = Group.from([10, 20]); | |
console.log(group.has(10)); | |
// → true | |
console.log(group.has(30)); | |
// → false | |
group.add(10); | |
group.delete(10); | |
console.log(group.has(10)); | |
// → false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment