FastPrepSingle Number with Triplicates

Single Number with Triplicates

Goldman Sachs logoGoldman Sachs● MediumFULLTIMEONSITE INTERVIEW
Learn

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

See Goldman Sachs hiring insights
public int singleNumberTriplicates(int[] nums) {
  // write your code here
}
nums[2,2,3,2]
expected3
Checking account…