Given an array of integers nums, find the largest integer that appears only once.
To clarify:
-1.Example 1:
Input: nums = [5,7,3,9,4,0,5,8,3] Output: 9 Explanation: There are unique numbers [7,9,4,0,8] The largest of these is 9.
Example 2:
Input: nums = [9,9,8,8] Output: -1 Explanation: There are no unique numbers.
Constraints:
1 <= nums.length <= 20000 <= nums[i] <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for finding the largest unique number means checking every number in the input to see if it's unique. We start with the largest possible number and work our way down, stopping as soon as we find a unique one.
Here's how the algorithm would work step-by-step:
def largest_unique_number_brute_force(numbers):
possible_answers = []
for number in numbers:
is_unique = True
# Check if the number appears more than once.
for other_number in numbers:
if number == other_number and numbers.index(number) != numbers.index(other_number):
is_unique = False
break
# If the number is unique, remember it.
if is_unique:
possible_answers.append(number)
# Need to handle the case where no number is unique.
if not possible_answers:
return -1
# Find the largest among unique numbers.
return max(possible_answers)To find the largest unique number in a list, we'll count how often each number appears. Then, we'll pick out the largest number that appears only once.
Here's how the algorithm would work step-by-step:
def largest_unique_number(numbers):
number_counts = {}
for number in numbers:
number_counts[number] = number_counts.get(number, 0) + 1
# Identify numbers appearing only once.
unique_numbers = [number for number, count in number_counts.items() if count == 1]
# Handle the case where there are no unique numbers.
if not unique_numbers:
return -1
# Find the largest among the unique numbers.
largest_unique = max(unique_numbers)
return largest_unique| Case | How to Handle |
|---|---|
| Empty input array | Return -1 if the input array is empty as there is no largest unique number. |
| Array with all duplicate numbers | Return -1 since no number is unique, indicating no valid solution. |
| Array with only one number | Return that single number if its count is one; otherwise, return -1. |
| Array with negative numbers only | The algorithm should correctly identify the largest negative number if it appears only once; otherwise return -1. |
| Array with large numbers | The solution should handle the numbers in a reasonable range avoiding integer overflow with appropriate data types. |
| Array with mixed positive, negative, and zero values | The solution must correctly identify the largest unique number regardless of sign. |
| Array with many duplicate values but one unique number | The algorithm needs to efficiently count the occurrence of each number and find the largest unique one. |
| Array with numbers close to the integer limit | Verify chosen data type can represent extreme numbers to avoid overflow/underflow during processing. |