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.
1import java.util.HashMap;
2import java.util.Map;
3
4public class Solution {
5 public static Integer biggestSingleNumber(int[] nums) {
6 Map<Integer, Integer> numCount = new HashMap<>();
7 for (int num : nums) {
8 numCount.put(num, numCount.getOrDefault(num, 0) + 1);
9 }
10
11 Integer maxNum = null;
12 for (Map.Entry<Integer, Integer> entry : numCount.entrySet()) {
13 if (entry.getValue() == 1) {
14 if (maxNum == null || entry.getKey() > maxNum) {
15 maxNum = entry.getKey();
16 }
17 }
18 }
19
20 return maxNum;
21 }
22
23 public static void main(String[] args) {
24 int[] nums = {8, 8, 3, 3, 1, 4, 5, 6};
25 Integer result = biggestSingleNumber(nums);
26 System.out.println(result != null ? result : "null");
27 }
28}
In the Java solution, we utilize a HashMap to track the frequency of each integer. A second loop is used to determine the largest integer appearing only once, and the result is returned.
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.
1import
The Java approach involves sorting the array and subsequently scanning it to find numbers that only appear once and return the largest one.