Sponsored
Sponsored
This approach involves using a hash map or dictionary to count the number of occurrences of each integer in the array. After counting, we identify numbers that appeared only once and find the maximum among them. This way, we can efficiently determine the largest single number.
Time Complexity: O(n^2) in worst case for counting and sorting (since sorting involves iterating the elements list).
Space Complexity: O(n) for storing numbers and their counts.
1#include <iostream>
2#include <map>
3#include <vector>
4using namespace std;
5
6int biggestSingleNumber(vector<int>& nums) {
7 map<int, int> numCount;
8 for (auto num : nums) {
9 numCount[num]++;
10 }
11
12 int maxNum = INT_MIN;
13 for (auto [num, count] : numCount) {
14 if (count == 1) {
15 maxNum = max(maxNum, num);
16 }
17 }
18
19 return maxNum == INT_MIN ? -1 : maxNum;
20}
21
22int main() {
23 vector<int> nums = {8, 8, 3, 3, 1, 4, 5, 6};
24 int result = biggestSingleNumber(nums);
25 if (result == -1) {
26 cout << "null" << endl;
27 } else {
28 cout << result << endl;
29 }
30 return 0;
31}
In this C++ solution, we use a map to store and count occurrences of each number. After counting, we iterate over the map to identify numbers appearing exactly once and keep track of the maximum among them.
This approach involves sorting the numbers first. After sorting, we can iterate through the list to count numbers that appear consecutively more than once and skip them. The largest number with only one occurrence is then the answer.
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) as we sort in place.
1def
This Python script first sorts the list in descending order. It then iterates through the sorted list, identifying the largest single number using simple comparisons.