/137. Single Number II

137. Single Number II

Medium
Arrays66.8% acceptance

Given an integer array values where every element appears exactly three times except for one element which appears exactly once, return the element that appears once. The solution must have linear runtime complexity and use only constant extra space.

Example 1

Input: [5,5,5,7,7,7,8]

Output: 8

Explanation: 8 appears once, all others appear three times.

Example 2

Input: [-10,-10,-10,0,0,0,42]

Output: 42

Explanation: 42 appears once, all others appear three times.

Example 3

Input: [100,100,100,-1,-1,-1,0]

Output: 0

Explanation: 0 appears once, all others appear three times.

Constraints

  • 1 <= len(values) <= 30000
  • -231 <= values[i] <= 231 - 1
  • All elements in values appear exactly three times except one element which appears exactly once.
  • Solution must use O(1) extra space and O(n) time.
Python (current runtime)

Case 1

Input: [4,4,4,9,9,9,11]

Expected: 11

Case 2

Input: [1,1,1,2,2,2,3]

Expected: 3

Case 3

Input: [-7,-7,-7,6,6,6,15]

Expected: 15

Case 4

Input: [0,0,0,-5,-5,-5,23]

Expected: 23

Case 5

Input: [8,8,8,12,12,12,-3]

Expected: -3