Talentd
FleetCode

3978. Unique Middle Element

Easy72.3% acceptance

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

Hints open one at a time

Hint 1
The middle index is nums.length / 2.
Hint 2
Count how many times nums[nums.length / 2] appears in nums.
2:30
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def isMiddleElementUnique(self, nums: list[int]) -> bool:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,2,3]
Input
[1,2,2]
Your code is saved on this device.Solution