3159. Find Occurrences of an Element in an Array LeetCode Solution

In this guide, you will get 3159. Find Occurrences of an Element in an Array LeetCode Solution with the best time and space complexity. The solution to Find Occurrences of an Element in an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Occurrences of an Element in an Array solution in C++
  4. Find Occurrences of an Element in an Array solution in Java
  5. Find Occurrences of an Element in an Array solution in Python
  6. Additional Resources
3159. Find Occurrences of an Element in an Array LeetCode Solution image

Problem Statement of 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:

Input: nums = [1,3,1,7], queries = [1,3,2,4], x = 1
Output: [0,-1,2,-1]
Explanation:

For the 1st query, the first occurrence of 1 is at index 0.
For the 2nd query, there are only two occurrences of 1 in nums, so the answer is -1.
For the 3rd query, the second occurrence of 1 is at index 2.
For the 4th query, there are only two occurrences of 1 in nums, so the answer is -1.

See also  1900. The Earliest and Latest Rounds Where Players Compete LeetCode Solution

Example 2:

Input: nums = [1,2,3], queries = [10], x = 5
Output: [-1]
Explanation:

For the 1st query, 5 doesn’t exist in nums, so the answer is -1.

Constraints:

1 <= nums.length, queries.length <= 105
1 <= queries[i] <= 105
1 <= nums[i], x <= 104

Complexity Analysis

  • Time Complexity: O(n + q)
  • Space Complexity: O(n)

3159. Find Occurrences of an Element in an Array LeetCode Solution in C++

class Solution {
 public:
  vector<int> occurrencesOfElement(vector<int>& nums, vector<int>& queries,
                                   int x) {
    const vector<int> indices = getIndices(nums, x);
    vector<int> ans;

    for (const int query : queries)
      ans.push_back(query <= indices.size() ? indices[query - 1] : -1);

    return ans;
  }

 private:
  vector<int> getIndices(const vector<int>& nums, int x) {
    vector<int> indices;
    for (int i = 0; i < nums.size(); ++i)
      if (nums[i] == x)
        indices.push_back(i);
    return indices;
  }
};
/* code provided by PROGIEZ */

3159. Find Occurrences of an Element in an Array LeetCode Solution in Java

class Solution {
  public int[] occurrencesOfElement(int[] nums, int[] queries, int x) {
    List<Integer> indices = getIndices(nums, x);
    int[] ans = new int[queries.length];

    for (int i = 0; i < queries.length; i++)
      ans[i] = queries[i] <= indices.size() ? indices.get(queries[i] - 1) : -1;

    return ans;
  }

  private List<Integer> getIndices(int[] nums, int x) {
    List<Integer> indices = new ArrayList<>();
    for (int i = 0; i < nums.length; ++i)
      if (nums[i] == x)
        indices.add(i);
    return indices;
  }
}
// code provided by PROGIEZ

3159. Find Occurrences of an Element in an Array LeetCode Solution in Python

class Solution:
  def occurrencesOfElement(
      self,
      nums: list[int],
      queries: list[int],
      x: int,
  ) -> list[int]:
    indices = [i for i, num in enumerate(nums) if num == x]
    return [indices[query - 1] if query <= len(indices) else -1
            for query in queries]
# code by PROGIEZ

Additional Resources

See also  122. Best Time to Buy and Sell Stock II LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.