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.
1def biggest_single_number(nums):
2 num_count = {}
3 for num in nums:
4 if num in num_count:
5 num_count[num] += 1
6 else:
7 num_count[num] = 1
8
9 max_num = float('-inf')
10 for num, count in num_count.items():
11 if count == 1:
12 max_num = max(max_num, num)
13
14 return None if max_num == float('-inf') else max_num
15
16numbers = [8, 8, 3, 3, 1, 4, 5, 6]
17result = biggest_single_number(numbers)
18print('null' if result is None else result)
This Python script uses a dictionary to tally occurrences of each number. We iterate over the dictionary to find numbers with only one occurrence and determine the largest 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.