Domain 1EasyCORE

Biggest Single Number

The problem asks us to find the largest single number in an array. A single number is defined as a number that appears exactly once in the array. If no such number exists, we should return -1.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 0

619. Biggest Single Number

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 0
Acceptance rate: 45.2%


Problem Breakdown

The problem asks us to find the largest single number in an array. A single number is defined as a number that appears exactly once in the array. If no such number exists, we should return -1.

Inputs:

  • An integer array nums.

Outputs:

  • The largest single number in the array, or -1 if no such number exists.

Implicit Constraints and Edge Cases:

  • The array can be empty.
  • All numbers are integers.
  • There might be negative numbers.
  • Duplicates are present, but we need to identify numbers that appear exactly once.

Intuition

A brute-force approach would involve iterating through the array multiple times. For each number, count its occurrences and keep track of the largest number with a count of one. This approach is inefficient because it has a time complexity of O(n^2) due to nested loops for counting occurrences.

The key insight is to use a hash map (or dictionary) to count the occurrences of each number in a single pass, reducing the time complexity to O(n). After populating the hash map, we can iterate through its keys to find the largest number with a count of one.

Approach

  1. Initialize an empty dictionary count_map to store the frequency of each number.
  2. Iterate through the array nums and populate count_map where the key is the number and the value is its count.
  3. Initialize a variable largest_single_number to -1.
  4. Iterate through the keys in count_map. For each key, if its value (count) is 1, update largest_single_number if this key is greater than the current largest_single_number.
  5. Return largest_single_number.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the array. We make two passes over the array: one to populate the dictionary and another to find the largest single number.
  • Space Complexity: O(n) in the worst case, where all numbers are unique.

Solution

def largest_single_number(nums):
    count_map = {}
    
    # Count occurrences of each number
    for num in nums:
        if num in count_map:
            count_map[num] += 1
        else:
            count_map[num] = 1
    
    largest_single_number = -1
    
    # Find the largest number with a count of one
    for num, count in count_map.items():
        if count == 1 and num > largest_single_number:
            largest_single_number = num
    
    return largest_single_number

Edge Cases Handled

  • Empty Array: If the input array is empty, the function returns -1 as expected.
  • All Duplicates: If all numbers in the array are duplicates, the function returns -1 because no single number exists.
  • Negative Numbers: The function correctly handles negative numbers by treating them like any other integer.
  • Single Element Array: If the array has only one element, that element is returned if it appears once, or -1 if it doesn’t.