Single Number - II

Bit Manipulation Problems Medium
  • This type of problem finds real-world applications in data analytics and cybersecurity
  • Companies often maintain log files for user activities for analysis
  • Most users will repeat certain activities multiple times, but some unusual activities (like a potential security breach) might only appear once
  • An algorithm that can efficiently identify these one-time events can alarm security systems about potential threats, triggering necessary preventive actions

Given an array nums where each integer in nums appears thrice except one. Find out the number that has appeared only once.

Examples:

Input : nums = [2, 2, 2, 3]

Output : 3

Explanation : The integers 3 has appeared only once.

Input : nums = [1, 0, 3, 0, 1, 1, 3, 3, 10, 0]

Output : 10

Explanation : The integers 10 has appeared only once.

Input : nums = [5, 0, 1, 10, 1, 1, 5, 5, 10, 10]

Constraints

  • 1 <= n <= 3*104
  • -231 <= nums[i] <= 231 - 1

Hints

  • For each bit position (0 to 31), traverse the array and count how many numbers have that bit set to 1. Use the modulo operation (count % 3) to identify whether the unique number has that bit set.
  • After computing the bit values for all positions, combine them to form the unique number. Ensure you handle negative numbers correctly by checking the most significant bit.

Company Tags

Airbnb Stripe Electronic Arts Optum Docker Western Digital Medtronic Epic Games Zomato Dropbox Cerner eBay Deloitte Nutanix Micron Technology Square Bungie Byju's ARM Wayfair Instacart Databricks Roblox JPMorgan Chase KPMG Google Microsoft Amazon Meta Apple Netflix Adobe