Easy
You are given an integer array nums.
An integer x is special if all occurrences of x in nums appear in a single contiguous block.
Return the number of distinct special integers in nums.
Example 1:
Input: nums = [1,2,2,1]
Output: 1
Explanation:
[1, 2], so it is special.Therefore, there is one special integer.
Example 2:
Input: nums = [3,3,1,2,2,1]
Output: 2
Explanation:
[0, 1], so it is special.[3, 4], so it is special.Therefore, there are two special integers.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100class Solution {
fun countSpecialIntegers(nums: IntArray): Int {
val seen = BooleanArray(101)
val special = BooleanArray(101)
var count = 0
for (i in nums.indices) {
if (i == 0 || nums[i] != nums[i - 1]) {
if (seen[nums[i]]) {
special[nums[i]] = false
} else {
seen[nums[i]] = true
special[nums[i]] = true
}
}
}
for (x in 1..100) {
if (special[x]) {
count++
}
}
return count
}
}