Count Values Excluding the First Two
Problem statement
Given an integer array numbers, count the entries whose values are different from numbers[0] and also different from numbers[1], if the second entry exists.
- Array indices are
0-based. - If the array contains only one entry, exclude only the value of
numbers[0]. - The first two values may be equal. Count qualifying entries individually, including repeated values.
Return the count. A solution with time complexity no worse than O(numbers.length^2) is sufficient.
Function
solution(numbers: int[]) → intExamples
Example 1
numbers = [4,3,2,3,2,5,4,3]return = 3numbers[0] = 4 and numbers[1] = 3. The entries different from both values are:
numbers[2] = 2numbers[4] = 2numbers[5] = 5
There are 3 qualifying entries.
Example 2
numbers = [3,3,1,1,3]return = 2The first two entries are both 3. The entries numbers[2] = 1 and numbers[3] = 1 are different from 3, so the answer is 2.
Example 3
numbers = [-2]return = 0numbers[0] = -2, and numbers[1] does not exist. The only entry equals -2, so the answer is 0.
Constraints
- For this exercise, assume
1 <= numbers.length <= 1000. - For this exercise, assume
-10^9 <= numbers[i] <= 10^9.