573. Count Occurrences of Element in Stack

EasyStackStack

Given a stack represented as an array and a value x, return how many times x appears in the stack. The input is JSON {stack, x}.

Input: JSON {stack, x}.

Output: Integer — the number of occurrences of x.

Examples

Example 1
Input: {"stack":[1,2,2,3,2],"x":2}
Output: 3
Explanation: 2 appears three times.
Example 2
Input: {"stack":[],"x":5}
Output: 0
Explanation: Empty stack has none.
Example 3
Input: {"stack":[7],"x":7}
Output: 1
Explanation: One occurrence.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →