class AllOne { #count_to_strings: Record | undefined> = {}; #string_to_count = new Map(); inc(key: string): void { const count = this.#string_to_count.get(key) || 0; if (this.#string_to_count.has(key) && count) { this.#string_to_count.set(key, count + 1); } else { this.#string_to_count.set(key, 1); } this.#count_to_strings[count]?.delete(key); let set = this.#count_to_strings[count + 1]; if (!set) { set = new Set(); this.#count_to_strings[count + 1] = set; } set.add(key); if (this.#count_to_strings[count]?.size === 0) { Reflect.deleteProperty(this.#count_to_strings, count); } } dec(key: string): void { const count = this.#string_to_count.get(key) || 0; if (count === 0) { return; } if (this.#string_to_count.has(key) && count) { this.#string_to_count.set(key, count - 1); } if (count === 1) this.#string_to_count.delete(key); this.#count_to_strings[count]?.delete(key); if (count - 1 > 0) { let set = this.#count_to_strings[count - 1]; if (!set) { set = new Set(); this.#count_to_strings[count - 1] = set; } set.add(key); } if (this.#count_to_strings[count]?.size === 0) { Reflect.deleteProperty(this.#count_to_strings, count); } } getMaxKey(): string { const num = Object.keys(this.#count_to_strings).at(-1); if (typeof num === "undefined") return ""; const set = this.#count_to_strings[num]; if (set) { for (const key of set) { return key; } } return ""; } getMinKey(): string { const num = Object.keys(this.#count_to_strings).at(0); if (typeof num === "undefined") return ""; const set = this.#count_to_strings[num]; if (set) { for (const key of set) { return key; } } return ""; } } export default AllOne;