Find Occurrences of an Element in an Array - Problem

Imagine you're a data analyst working with search logs, and you need to quickly find the nth occurrence of specific search terms across your database. This problem captures that exact scenario!

You are given an integer array nums, an integer array queries, and a target integer x. For each query queries[i], you need to find the index where the queries[i]-th occurrence of x appears in the nums array.

Key Rules:

  • If there are fewer than queries[i] occurrences of x, return -1 for that query
  • Occurrences are counted from left to right (1st, 2nd, 3rd, etc.)
  • Return an integer array containing answers to all queries

Example: If nums = [1, 3, 1, 7, 1], x = 1, and queries = [1, 3, 2], then the 1st occurrence of 1 is at index 0, the 3rd occurrence is at index 4, and the 2nd occurrence is at index 2. Answer: [0, 4, 2]

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,3,1,7,1], queries = [1,3,2], x = 1
โ€บ Output: [0,4,2]
๐Ÿ’ก Note: The 1st occurrence of x = 1 is at index 0. The 3rd occurrence of x = 1 is at index 4. The 2nd occurrence of x = 1 is at index 2.
example_2.py โ€” Query Exceeds Occurrences
$ Input: nums = [1,2,3], queries = [10], x = 5
โ€บ Output: [-1]
๐Ÿ’ก Note: x = 5 doesn't appear in nums, so the 10th occurrence doesn't exist. Return -1.
example_3.py โ€” Mixed Valid and Invalid Queries
$ Input: nums = [5,2,5,4], queries = [2,4], x = 5
โ€บ Output: [2,-1]
๐Ÿ’ก Note: The 2nd occurrence of x = 5 is at index 2. The 4th occurrence doesn't exist since x only appears twice.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค queries.length โ‰ค 105
  • 1 โ‰ค queries[i] โ‰ค 105
  • 1 โ‰ค nums[i], x โ‰ค 109

Visualization

Tap to expand
๐Ÿ“š Library Catalog SystemLibrary Shelves (nums array)Harry PotterShelf 0Other BookHarry PotterShelf 2DifferentHarry PotterShelf 4๐Ÿ“‹ Index CreationStep 1: Walk through shelvesFound 'Harry Potter' at:โ€ข Shelf 0 (1st copy)โ€ข Shelf 2 (2nd copy)โ€ข Shelf 4 (3rd copy)โ“ Query ProcessingCustomer: "Where is 2nd copy?"Librarian: *checks index*Answer: "Shelf 2!" โœ“Time: Instant O(1) lookup๐Ÿšซ Without Index (Brute Force)Customer: "Where is 2nd copy?"Librarian: *walks entire library counting copies* "Let me check shelf 0... not 2nd... shelf 2... this is 2nd!"Problem: Must walk entire library for EACH customer question!
Understanding the Visualization
1
Create Index
Walk through the library once and note all positions where 'Harry Potter' appears
2
Answer Queries
When someone asks for the 2nd copy, instantly look up position from our index
3
Handle Missing
If someone asks for the 5th copy but we only have 3, return 'Not Found'
Key Takeaway
๐ŸŽฏ Key Insight: Preprocessing data once allows us to answer multiple queries efficiently, transforming O(nร—m) brute force into O(n+m) optimal solution!
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
42.5K Views
Medium Frequency
~15 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen