Mid/SeniorArrayHash Table

Find Occurrences of an Element in an Array

You are given an integer array nums, an integer array queries, and an integer x.

For each queries[i], you need to find the index of the queries[i]^th occurrence of x in the nums array. If there are fewer than queries[i] occurrences of x, the answer should be -1 for that query.

Return an integer array answer containing the answers to all queries.

Example 1
Inputnums = [1,3,1,7], queries = [1,3,2,4], x = 1
Output[0,-1,2,-1]
For the queries asking for the 1st, 3rd, 2nd, and 4th occurrences of 1, the answers are index 0, -1, index 2, and -1 respectively because 1 appears only twice.
Example 2
Inputnums = [1,2,3], queries = [10], x = 5
Output[-1]
Since 5 does not exist in nums, the answer for the only query is -1.

Constraints

  • 1 <= nums.length, queries.length <= 10^5
  • 1 <= queries[i] <= 10^5
  • 1 <= nums[i], x <= 10^4

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate