3159. Find Occurrences of an Element in an Array Medium
1/**
2 * [3159] Find Occurrences of an Element in an Array
3 *
4 * You are given an integer array nums, an integer array queries, and an integer x.
5 * 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.
6 * Return an integer array answer containing the answers to all queries.
7 *
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [1,3,1,7], queries = [1,3,2,4], x = 1</span>
11 * Output: <span class="example-io">[0,-1,2,-1]</span>
12 * Explanation:
13 *
14 * For the 1^st query, the first occurrence of 1 is at index 0.
15 * For the 2^nd query, there are only two occurrences of 1 in nums, so the answer is -1.
16 * For the 3^rd query, the second occurrence of 1 is at index 2.
17 * For the 4^th query, there are only two occurrences of 1 in nums, so the answer is -1.
18 * </div>
19 * <strong class="example">Example 2:
20 * <div class="example-block">
21 * Input: <span class="example-io">nums = [1,2,3], queries = [10], x = 5</span>
22 * Output: <span class="example-io">[-1]</span>
23 * Explanation:
24 *
25 * For the 1^st query, 5 doesn't exist in nums, so the answer is -1.
26 * </div>
27 *
28 * Constraints:
29 *
30 * 1 <= nums.length, queries.length <= 10^5
31 * 1 <= queries[i] <= 10^5
32 * 1 <= nums[i], x <= 10^4
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/find-occurrences-of-an-element-in-an-array/
38// discuss: https://leetcode.com/problems/find-occurrences-of-an-element-in-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn occurrences_of_element(nums: Vec<i32>, queries: Vec<i32>, x: i32) -> Vec<i32> {
44 vec![]
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_3159() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.