Problem Description
Given an integer array nums, return the largest integer that only occurs once in the array. If no integer occurs once, return -1.
Key Insights
- Use a hash table (or dictionary) to count the frequency of each number.
- Iterate over the hash table to find the numbers that appear exactly once.
- Identify the maximum number among the unique numbers.
- Return -1 if no unique number exists.
Space and Time Complexity
Time Complexity: O(n), where n is the number of elements in nums.
Space Complexity: O(n), for the hash table storing frequency counts.
Solution
The approach starts by initializing a hash table to keep track of the frequency of each element in the array. Once the frequency count is complete, we traverse the hash table to collect numbers that appear exactly once. Finally, we determine the maximum number among these unique elements. If there is no unique element, we return -1. This method is efficient because the frequency count and the search for the maximum unique element both run in linear time relative to the input size.