The Biggest Single Number problem requires you to find the largest integer in a list that appears exactly once. Given an array of integers nums
, return the biggest number that occurs only once. If no such number exists, return -1
.
nums
is an integer (positive, negative, or zero).-1
.At first glance, you might think of checking each number and counting how many times it appears in the array. For every number, you could count its occurrences and, if it appears only once, keep track of the largest such number. However, this brute-force approach would be inefficient, especially for large arrays, because it would require scanning the entire array for every unique number.
To optimize, we need a way to efficiently determine how many times each number appears. This leads us to the idea of using a data structure that can count occurrences quickly, such as a hash map (dictionary). With a hash map, we can count all occurrences in a single pass, then scan through the map to find the largest number with a count of one.
Let's break down the solution step by step:
nums
.-1
.This approach is efficient because counting is done in O(n) time, and finding the maximum is O(m), where m is the number of unique numbers (at most n).
Let's look at a sample input:
nums = [5, 3, 9, 3, 5, 7, 8]
If the input was [4, 4, 4, 4]
, then no number appears exactly once, so we would return -1
.
class Solution:
def singleNumber(self, nums):
from collections import Counter
count = Counter(nums)
max_unique = -1
for num in count:
if count[num] == 1:
max_unique = max(max_unique, num)
return max_unique
#include <unordered_map>
#include <vector>
#include <algorithm>
class Solution {
public:
int singleNumber(std::vector<int>& nums) {
std::unordered_map<int, int> count;
for (int num : nums) {
count[num]++;
}
int max_unique = -1;
for (auto& p : count) {
if (p.second == 1) {
max_unique = std::max(max_unique, p.first);
}
}
return max_unique;
}
};
import java.util.*;
class Solution {
public int singleNumber(int[] nums) {
Map<Integer, Integer> count = new HashMap<>();
for (int num : nums) {
count.put(num, count.getOrDefault(num, 0) + 1);
}
int maxUnique = -1;
for (int num : count.keySet()) {
if (count.get(num) == 1) {
maxUnique = Math.max(maxUnique, num);
}
}
return maxUnique;
}
}
var singleNumber = function(nums) {
const count = {};
for (const num of nums) {
count[num] = (count[num] || 0) + 1;
}
let maxUnique = -1;
for (const num in count) {
if (count[num] === 1) {
maxUnique = Math.max(maxUnique, Number(num));
}
}
return maxUnique;
};
Brute-force Approach:
The optimized approach is much more efficient, especially for large arrays, because it avoids repeated scanning.
The key insight for the Biggest Single Number problem is to efficiently count the occurrences of each number using a hash map, then select the largest number that appears exactly once. This approach is both simple and efficient, requiring only two passes over the data. It avoids unnecessary repeated work and leverages the power of hash maps for fast lookups and updates. The result is a solution that is easy to understand, implement, and scales well with input size.