Count Array Positions Whose Values Appear in the Other Array

Quick Overview

Count how many positions in each input array have values appearing in the other array, preserving duplicate indices and the order of the two counts.

Count Array Positions Whose Values Appear in the Other Array

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Count Array Positions Whose Values Appear in the Other Array You are given integer arrays `nums1` and `nums2` with lengths `n` and `m`. Calculate two directional counts: - `answer1` is the number of indices `i` in `nums1` for which `nums1[i]` appears at least once in `nums2`. - `answer2` is the number of indices `j` in `nums2` for which `nums2[j]` appears at least once in `nums1`. Return `[answer1, answer2]` in that order. ### Examples **Example 1** ```text Input: nums1 = [6,2,3], nums2 = [6,6,6,6,9] Output: [1,4] ``` Only index `0` of `nums1` qualifies. In `nums2`, indices `0`, `1`, `2`, and `3` all qualify because value `6` occurs in `nums1`. One occurrence in the other array can establish membership for several indices; it is not consumed. **Example 2** ```text Input: nums1 = [4,4,7,9], nums2 = [7,8,4] Output: [3,2] ``` Indices `0`, `1`, and `2` in `nums1` have values that occur in `nums2`. Indices `0` and `2` in `nums2` have values that occur in `nums1`. Both occurrences of `4` in the first array count independently. ### Constraints - `n == nums1.length` and `m == nums2.length`. - `1 <= n, m <= 100`. - `1 <= nums1[i], nums2[j] <= 100` for every valid index. ### Clarifying Questions - Are we counting distinct common values? No; count qualifying indices in each original array. - Does every matching pair of indices contribute separately? No; each index contributes at most once, regardless of how many matches exist in the other array. - Are matching occurrences consumed? No; these are independent membership counts, not one-to-one matching. - Can the two answers differ? Yes; the two arrays may contain different numbers of qualifying positions. - Is the result order arbitrary? No; return the count for `nums1` first and the count for `nums2` second. ### C++ Interface ```cpp class Solution { public: vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) { } }; ``` ### Reasoning Checkpoints - Preserve duplicate positions while testing membership in the opposite array. - Separate the two directional counts and keep their output order fixed. - Discuss the time and space costs under the stated input bounds. ```hint Store presence, then count original indices A presence table records whether a value occurs in an array. After building one table for each array, scan the original arrays so repeated positions still contribute independently. ```

Overview: Count how many positions in each input array have values appearing in the other array, preserving duplicate indices and the order of the two counts.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Count Array Positions Whose Values Appear in the Other Array

You are given integer arrays nums1 and nums2 with lengths n and m.

Calculate two directional counts:

  • answer1 is the number of indices i in nums1 for which nums1[i] appears at least once in nums2 .
  • answer2 is the number of indices j in nums2 for which nums2[j] appears at least once in nums1 .

Return [answer1, answer2] in that order.

Examples

Example 1

Input: nums1 = [6,2,3], nums2 = [6,6,6,6,9]
Output: [1,4]

Only index 0 of nums1 qualifies. In nums2, indices 0, 1, 2, and 3 all qualify because value 6 occurs in nums1. One occurrence in the other array can establish membership for several indices; it is not consumed.

Example 2

Input: nums1 = [4,4,7,9], nums2 = [7,8,4]
Output: [3,2]

Indices 0, 1, and 2 in nums1 have values that occur in nums2. Indices 0 and 2 in nums2 have values that occur in nums1. Both occurrences of 4 in the first array count independently.

Constraints

  • n == nums1.length and m == nums2.length .
  • 1 <= n, m <= 100 .
  • 1 <= nums1[i], nums2[j] <= 100 for every valid index.

Clarifying Questions Guidance

  • Are we counting distinct common values? No; count qualifying indices in each original array.
  • Does every matching pair of indices contribute separately? No; each index contributes at most once, regardless of how many matches exist in the other array.
  • Are matching occurrences consumed? No; these are independent membership counts, not one-to-one matching.
  • Can the two answers differ? Yes; the two arrays may contain different numbers of qualifying positions.
  • Is the result order arbitrary? No; return the count for nums1 first and the count for nums2 second.

C++ Interface

class Solution {
public:
    vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
        
    }
};

Reasoning Checkpoints

  • Preserve duplicate positions while testing membership in the opposite array.
  • Separate the two directional counts and keep their output order fixed.
  • Discuss the time and space costs under the stated input bounds.