Single Number - III

Bit Manipulation Problems Medium

Given an array nums of length n, every integer in the array appears twice except for two integers. Identify and return the two integers that appear only once in the array. Return the two numbers in ascending order.


For example, if nums = [1, 2, 1, 3, 5, 2], the correct answer is [3, 5], not [5, 3].

Examples:

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

Output : [3, 5]

Explanation : The integers 3 and 5 have appeared only once.

Input : nums = [-1, 0]

Output : [-1, 0]

Explanation : The integers -1 and 0 have appeared only once.

Input : nums = [1, 9, 1, 2, 8, 2]

Constraints

  • 2 <= nums.length <= 105
  • -3*105 <= nums[i] <= 3*105
  • Every integer in nums appears twice except two integers.

Hints

  • The XOR result, x⊕y, will have at least one bit set to 1. This bit represents a position where x and y differ. Isolate this bit by using diff=xor_result&−xor_result, which gives the rightmost set bit.
  • Partition the array into two groups based on whether the numbers have the differentiating bit set (1) or unset (0). One group will contain x, and the other will contain y. XOR the elements in each group to isolate x and y.

Company Tags

Seagate Technology Zoho Qualcomm Byju's Micron Technology Roche Walmart HCL Technologies Databricks Bloomberg Shopify Airbnb MongoDB JPMorgan Chase Twilio Freshworks Square OYO Rooms Boston Consulting Group Dropbox Morgan Stanley KPMG Visa Lyft NVIDIA Google Microsoft Amazon Meta Apple Netflix Adobe