Count Elements With a Next
Count the items x in arr such that x + 1 also appears somewhere in arr. If a value is repeated, count each copy separately.
Example 1
arr [1,2,3]
returns 2
1 (2 is present) and 2 (3 is present) count; 3 does not.
Example 2
arr [1,1,3,3,5,5,7,7]
returns 0
No item has its successor in the list.
Constraints
1 <= len(arr) <= 40-20 <= arr[i] <= 20
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Hash maps problems
- Difference of Two ArraysBeginner
- First Repeated WordBeginner
- Is IsogramBeginner
- Jewels and StonesBeginner
- Keep First of Each LetterBeginner
- Letter Count TableBeginner
- Matching Sock PairsBeginner
- Translate With a GlossaryBeginner