Algorithm - count elements of an Array

Algorithm - count elements of an Array

Video

Suscríbete



Exercise

Given an Integer arr, count how many elements x there are, such that x is also in arr. If there're duplicates in arr, count them separately.


Example 1

Input: arr = [1,2,3]
Output: 2

Explanation:

There are 2 such elements. 1 and 2 because 2 and 3 are also in the array.


Example 2

Input: arr = [1,1,3,3,5,5,7,7]
Output: 0

Explanation:

There's no such number because 2, 4, 6 or 8 is not present in the array.


Example 3

Input: arr = [1,3,2,3,5,0]
Output: 0

Explanation:

There's no such number because 2, 4, 6 or 8 is not present in the array.


Example 4

Input: arr = [1,1,2,2]
Output: 2

Explanation:

There are 2 such numbers, and 1 and 1 because 2 is in the array and duplicates are counted separately.


Example 5

Input: arr = [1,1,2]
Output: 2

Explanation:

There are 2 such numbers, and 1 and 1 because 2 is in the array and duplicates are counted separately.


Try the code


vladi.codes