Skip to content

Instantly share code, notes, and snippets.

@yitonghe00
Last active July 27, 2021 23:38
Show Gist options
  • Save yitonghe00/fae0a8adb4c1688523af12809ed734ff to your computer and use it in GitHub Desktop.
Save yitonghe00/fae0a8adb4c1688523af12809ed734ff to your computer and use it in GitHub Desktop.
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