Skip to content

1133. Largest Unique Number 👍

  • Time: $O(n)$
  • Space: $O(1000) = O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
 public:
  int largestUniqueNumber(vector<int>& nums) {
    constexpr int kMax = 1000;
    vector<short> count(kMax + 1);

    for (const int num : nums)
      ++count[num];

    for (int num = kMax; num >= 0; --num)
      if (count[num] == 1)
        return num;

    return -1;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
  public int largestUniqueNumber(int[] nums) {
    final int kMax = 1000;
    int[] count = new int[kMax + 1];

    for (final int num : nums)
      ++count[num];

    for (int num = kMax; num >= 0; --num)
      if (count[num] == 1)
        return num;

    return -1;
  }
}
1
2
3
4
class Solution:
  def largestUniqueNumber(self, nums: list[int]) -> int:
    return max([num for num, freq in collections.Counter(nums).items()
                if freq == 1], default=-1)