"""Counting Elements

URL: https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/528/week-1/3289/

Given an integer array arr, count element x such that x + 1 is also in arr.

If there're duplicates in arr, count them seperately.

Example 1:
Input: arr = [1,2,3]
Output: 2
Explanation: 1 and 2 are counted cause 2 and 3 are in arr.

Example 2:
Input: arr = [1,1,3,3,5,5,7,7]
Output: 0
Explanation: No numbers are counted, cause there's no 2, 4, 6, or 8 in arr.

Example 3:
Input: arr = [1,3,2,3,5,0]
Output: 3
Explanation: 0, 1 and 2 are counted cause 1, 2 and 3 are in arr.

Example 4:
Input: arr = [1,1,2,2]
Output: 2
Explanation: Two 1s are counted cause 2 is in arr.

Constraints:
- 1 <= arr.length <= 1000
- 0 <= arr[i] <= 1000
"""


class Solution(object):
    def countElements(self, arr):
        """
        :type arr: List[int]
        :rtype: int
        """
        pass


def main():
    pass


if __name__ == '__main__':
    main()

@@ -749,16 +749,20 @@
 00%0A%22%22%22%0A%0A
+    
 %0Aclass S
@@ -768,16 +768,27 @@
 Solution
+NumCountict
 (object)
@@ -888,55 +888,901 @@
 int%0A
-        %22%22%22%0A        pass%0A%0A%0Adef main():%0A    pass
+%0A        Time complexity: O(n).%0A        Space complexity: O(n).%0A        %22%22%22%0A        from collections import defaultdict%0A        %0A        # Edge case.%0A        if len(arr) == 1:%0A            return 0%0A        %0A        # Create a dict:number-%3Ecount.%0A        num_count_d = defaultdict(int)%0A        for num in arr:%0A            num_count_d%5Bnum%5D += 1%0A        %0A        result = 0%0A        for num, count in num_count_d.items():%0A            if num + 1 in num_count_d:%0A                result += count%0A        %0A        return result%0A%0A%0Adef main():%0A    # Output: 2%0A    arr = %5B1,2,3%5D%0A    print SolutionNumCountict().countElements(arr)%0A%0A    # Output: 0%0A    arr = %5B1,1,3,3,5,5,7,7%5D%0A    print SolutionNumCountict().countElements(arr)%0A%0A    # Output: 3%0A    arr = %5B1,3,2,3,5,0%5D%0A    print SolutionNumCountict().countElements(arr)%0A%0A    # Output: 2%0A    arr = %5B1,1,2,2%5D%0A    print SolutionNumCountict().countElements(arr)
 %0A%0A%0Ai

Complete num count dict sol