Problem · Array

Single Number with Triplicates

Learn this problem
MediumGoldman Sachs logoGoldman SachsFULLTIMEONSITE INTERVIEW

Problem statement

Given a nonempty integer array nums, every value appears exactly three times except for one value that appears once. Return that single value.

Your solution must run in O(n) time and use O(1) auxiliary space.

Function

singleNumberTriplicates(nums: int[]) → int

Examples

Example 1

nums = [2,2,3,2]return = 3

Only 3 occurs once.

Example 2

nums = [0,1,0,1,0,1,99]return = 99

Zero and one occur three times; 99 occurs once.

Constraints

  • 1 <= nums.length <= 300001
  • Every value fits in a signed 32-bit integer.
  • Exactly one value appears once; every other distinct value appears three times.

More Goldman Sachs problems

drafts saved locally
public int singleNumberTriplicates(int[] nums) {
  // write your code here
}
nums[2,2,3,2]
expected3
checking account