JavaScript has no TreeSet. Build an OrderedMultiset of numbers that keeps every copy of a value and answers ordered queries.
add(x) adds one copy of xremove(x) removes one copy and returns true, or returns false if there is nonehas(x), and size as a getterfloor(x): the largest value <= x; lower(x): the largest value < xceiling(x): the smallest value >= x; higher(x): the smallest value > x- each of those four returns
undefined when there is no such value rank(x): how many stored values are smaller than x (it works whether or not x is stored)countRange(lo, hi): how many stored values lie in [lo, hi], both ends includedat(i): the i-th smallest value counting from 0, or undefined if i is out of range
Aim for O(log n) per operation (a balanced tree, buckets, or a binary search over a sorted array if you accept an O(n) shift). A plain unbalanced search tree will fail the sorted-input test.