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.
1using System;
2using System.Collections.Generic;
3
4class Program {
5 public static int? BiggestSingleNumber(int[] nums) {
6 Dictionary<int, int> numCount = new Dictionary<int, int>();
7 foreach (int num in nums) {
8 if (numCount.ContainsKey(num)) {
9 numCount[num]++;
10 } else {
11 numCount[num] = 1;
12 }
13 }
14
15 int? maxNum = null;
16 foreach (var pair in numCount) {
17 if (pair.Value == 1) {
18 if (maxNum == null || pair.Key > maxNum) {
19 maxNum = pair.Key;
20 }
21 }
22 }
23
24 return maxNum;
25 }
26
27 static void Main() {
28 int[] nums = {8, 8, 3, 3, 1, 4, 5, 6};
29 int? result = BiggestSingleNumber(nums);
30 Console.WriteLine(result != null ? result.ToString() : "null");
31 }
32}
The C# implementation applies a dictionary to store counts of each integer, checking later for the largest single occurrence via a loop.
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.
1using System;
using System.Linq;
class Program {
public static int? BiggestSingleNumber(int[] nums) {
if (nums.Length == 0) return null;
Array.Sort(nums);
for (int i = nums.Length - 1; i >= 0; i--) {
if ((i == 0 || nums[i] != nums[i - 1]) &&
(i == nums.Length - 1 || nums[i] != nums[i + 1])) {
return nums[i];
}
}
return null;
}
static void Main() {
int[] nums = {8, 8, 3, 3, 1, 4, 5, 6};
int? result = BiggestSingleNumber(nums);
Console.WriteLine(result != null ? result.ToString() : "null");
}
}
The C# approach sorts the integers, searches for unique occurrences, and delivers the largest one for output.