Add Values That Occur Once

Quick Overview

Add Values That Occur Once. Return the sum of those values in nums that appear exactly once. Values with two or more occurrences contribute nothing.

Add Values That Occur Once

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Return the sum of those values in `nums` that appear exactly once. Values with two or more occurrences contribute nothing. ```cpp class Solution { public: int sumOfUnique(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text nums = [4,2,4,7,2,9] Output: 16 Explanation: Only 7 and 9 appear once. ``` **Example 2:** ```text nums = [6,6,3,3] Output: 0 Explanation: Every value repeats. ``` ### Constraints - `1 <= nums.length <= 100`. - Each value is an integer in `[1,100]`. ### Clarifying Questions Unique means frequency exactly one, rather than one copy of every distinct value. ```hint Inspect the structure Does a repeated value contribute one copy, all copies, or no contribution under the stated definition? ``` ### Reasoning checkpoints - Include a value only when it occurs exactly once in the complete input. - Exclude every repeated value completely. ### Optional extensions How could an incremental sum adjust when a value appears for the second time?

Overview: Add Values That Occur Once. Return the sum of those values in nums that appear exactly once. Values with two or more occurrences contribute nothing.

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

Problem

Return the sum of those values in nums that appear exactly once. Values with two or more occurrences contribute nothing.

class Solution {
public:
    int sumOfUnique(vector<int>& nums);
};

Examples

Example 1:

nums = [4,2,4,7,2,9]
Output: 16
Explanation: Only 7 and 9 appear once.

Example 2:

nums = [6,6,3,3]
Output: 0
Explanation: Every value repeats.

Constraints

  • 1 <= nums.length <= 100 .
  • Each value is an integer in [1,100] .

Clarifying Questions Guidance

Unique means frequency exactly one, rather than one copy of every distinct value.

Reasoning checkpoints

  • Include a value only when it occurs exactly once in the complete input.
  • Exclude every repeated value completely.

Optional extensions

How could an incremental sum adjust when a value appears for the second time?