Skip to main content

Unique Middle Element - Solution & Explanation

EasyArrayCounting4 min read
Practice this problem

Problem Statement

You are given an integer array nums of odd length n.

Return true if the middle element of nums appears exactly once in the array. Otherwise return false.

 

Example 1:

Input: nums = [1,2,3]

Output: true

Explanation:

The middle element of nums is 2, which appears exactly once.

Thus, the answer is true.

Example 2:

Input: nums = [1,2,2]

Output: false

Explanation:

The middle element of nums is 2, which appears twice.

Thus, the answer is false.

 

Constraints:

  • 1 <= n == nums.length <= 100
  • n is odd.
  • 1 <= nums[i] <= 100

Approach Overview

Problem Overview: You need to identify the middle element that appears exactly once while the remaining values may repeat multiple times. The challenge is separating the unique value efficiently without scanning the array repeatedly.

Approach 1: Brute Force Frequency Scan (O(n²) time, O(1) space)

Iterate through every element and count its occurrences by scanning the entire array again. After computing the frequency for the current value, check whether it is unique and whether it satisfies the middle-position condition from the problem statement. This approach avoids extra memory and works well for very small inputs, but repeated comparisons make it slow for larger arrays. Problems involving repeated counting are common in arrays practice sets.

Approach 2: Hash Map Frequency Counting (O(n) time, O(n) space)

Store element frequencies in a hash map using one linear pass. Once frequencies are available, iterate through the array again and return the element whose count equals 1 and matches the required middle index logic. The key insight is that hash lookups reduce repeated counting from linear time to constant average time. This is the standard solution interviewers expect for easy frequency problems involving hash tables.

Approach 3: Sorting and Neighbor Comparison (O(n log n) time, O(1) or O(n) space)

Sort the array first, then scan linearly while comparing neighboring values. A value is unique if it differs from both adjacent elements after sorting. This method is useful when the input is already sorted or when hash-based structures are restricted. The tradeoff is the sorting overhead, but the implementation stays simple and deterministic. Similar techniques appear in sorting and duplicate-removal problems.

Recommended for interviews: The hash map solution is the strongest choice because it gives linear performance with straightforward logic. Interviewers usually expect you to recognize the frequency-counting pattern quickly and optimize away nested loops. Showing the brute force approach first demonstrates baseline reasoning, while moving to the O(n) hash map version shows optimization skill and awareness of time complexity tradeoffs.

Solution

We take the element at the middle index of the array and count how many times it appears. If the count is 1, return true; otherwise return false.

The time complexity is O(n), and the space complexity is O(1), where n is the length of the array nums.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Frequency ScanO(n²)O(1)Small inputs or memory-constrained environments
Hash Map Frequency CountingO(n)O(n)General case and interview settings
Sorting and Neighbor ComparisonO(n log n)O(1) to O(n)When the array is already sorted or hashing is unavailable

Video Solution

3978. Unique Middle Element (Leetcode Easy)Programming Live with Larry59 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Unique Middle Element easy or hard?
Unique Middle Element is generally considered an easy problem because the core idea is straightforward frequency counting. The main interview expectation is recognizing when to replace nested loops with hashing to improve performance from O(n²) to O(n).
Unique Middle Element Python/Java solution
Python solutions typically use collections.Counter or a standard dictionary for frequency tracking. Java implementations usually rely on HashMap<Integer, Integer>. Both approaches achieve O(n) time complexity with clean and readable code.
How to solve Unique Middle Element in O(n)?
Use a hash map to count how many times each value appears in the array. After building the frequency table, iterate through the elements and return the value whose frequency is exactly one and satisfies the middle-element condition. Hash lookups work in constant average time, giving an overall O(n) solution.
What is the best approach for Unique Middle Element?
The hash map frequency counting approach is the best overall solution for Unique Middle Element. It processes the array in linear time by storing frequencies in a dictionary or hash map, then identifies the unique middle value in a second pass. The total complexity is O(n) time and O(n) space.
Is Unique Middle Element asked at Google/Amazon/Meta?
Frequency-counting and unique-element problems are common screening questions at companies such as Amazon, Google, and Meta because they test array traversal, hashing, and complexity optimization. The exact problem title may vary, but the underlying pattern appears frequently in coding interviews.
What data structure is used in Unique Middle Element?
The most common data structure used is a hash map or dictionary for storing element frequencies. Some alternative solutions use sorting with array traversal, but hashing gives the best runtime for unsorted inputs.
What is the time complexity of Unique Middle Element?
The optimal solution runs in O(n) time using a hash map for frequency counting. A brute force implementation that counts occurrences for every element separately takes O(n²) time. Sorting-based solutions usually run in O(n log n).

Ready to solve this problem?

Practice Unique Middle Element with our built-in code editor and test cases.

Practice on FleetCode