AniUI Academy
easy+150 XPPractice

A set built on a plain object

Before Set existed, every codebase kept an object as a lookup table: selected tag ids, dismissed banner keys, feature flags already seen. It is still the shape you get back from JSON, so knowing how to do it safely matters.

Implement a UniqueSet class backed by a plain object rather than the built-in Set. It is constructed with no arguments and starts empty. add(value) stores a value and returns true if it was newly added or false if it was already present. has(value) returns true or false. delete(value) removes a value and returns true if it was present or false if it was not. size() returns how many distinct values are stored. values() returns an array of the stored values in the order they were first added, and returns [] when empty.

Every value is a word-like string such as a tag name — never a numeric string like '12'. Watch out for the classic trap: an empty object still answers to keys like toString and constructor through its prototype, so has('toString') must be false on an empty set.

What it has to do

  • add returns true the first time a value is stored and false on a repeat.
  • has and delete return false for inherited property names such as toString and constructor.
  • values() lists the stored values in first-added order.
  • values() returns an empty array and size() returns 0 for a new set.
  • Do not use the built-in Set or Map.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.