[LeetCode] Largest Unique Number

1133. Largest Unique Number

Given an integer array nums, return the largest integer that only occurs once. If no integer occurs once, return -1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int largestUniqueNumber(vector<int>& nums) {
sort(begin(nums), end(nums));
while(nums.size()) {
int x = nums.back(), cnt = 0;
while(nums.size() and nums.back() == x) {
nums.pop_back();
cnt += 1;
}
if(cnt == 1) return x;
}
return -1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/07/30/PS/LeetCode/largest-unique-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.