[LeetCode] Unique Middle Element

3978. Unique Middle Element

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
bool isMiddleElementUnique(vector<int>& nums) {
int n = nums.size(), mid = n / 2;
for(int i = 0; i < n; i++) {
if(i == mid) continue;
if(nums[i] == nums[mid]) return false;
}
return true;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/unique-middle-element/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.