searchsorted + bincount would have done the trick :)