Problem 505366 · medium · Phase 05 Advanced Algorithms & Graphs

The Crate That Arrived Only Once

bit manipulation · counting modulo 3

A warehouse logs crate codes in codes. Every code was delivered exactly three times, except one code that was delivered once. Return that code. Codes may be negative.

Try to use O(1) extra space: no dictionaries or sets.

Examples

Input:  codes = [6, 1, 6, 6]
Output: 1
Input:  codes = [-4, 9, 9, -4, 9, -4, 12]
Output: 12

Constraints

  • 1 <= len(codes) <= 10**5 + 1, -2**31 <= codes[i] < 2**31.
  • Exactly one code appears once; all others appear three times.
  • Target complexity: O(n) time, O(1) extra space.

Goals

  • Count each bit position modulo 3
  • Maintain the modulo-3 counters with two bit masks in O(1) space
  • Handle negative numbers correctly
Starting Python…