Problem statement

https://binarysearch.com/problems/Count-Next-Element/

Solution

Just use counter here.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, nums):
        s = Counter(nums)
        ans = 0
        for x in s:
            if s[x + 1] > 0:
                ans += s[x]
        return ans