Easy
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 <= 100n is odd.1 <= nums[i] <= 100public class Solution {
public boolean isMiddleElementUnique(int[] nums) {
int mid = nums.length / 2;
int key = nums[mid];
int freq = 0;
for (int ele : nums) {
if (ele == key) {
freq++;
}
}
return (freq == 1);
}
}