260. Single Number III Leetcode Solution

260. Single Number III Leetcode Solution

Difficulty: Medium

Topics: Array, Bit Manipulation

Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.

You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.

Example 1:

  • Input: nums = [1,2,1,3,2,5]
  • Output: [3,5]
  • Explanation: [5, 3] is also a valid answer.

Example 2:

  • Input: nums = [-1,0]
  • Output: [-1,0]

Example 3:

  • Input: nums = [0,1]
  • Output: [1,0]

Constraints:

  • <code>2 <= nums.length <= 3 * 104
  • <code>-231<code> <= nums[i] <= 231 - 1
  • Each integer in nums will appear twice, only two integers will appear once.

Solution:

We can use bit manipulation, specifically the XOR operation. The key idea is that XORing two identical numbers results in 0, and XORing a number with 0 gives the number itself. Using this property, we can find the two unique numbers that appear only once in the array.

Step-by-Step Solution:

  1. XOR all elements in the array. The result will be the XOR of the two unique numbers because all other numbers will cancel out (since they appear twice).
  2. Find a set bit in the XOR result (this bit will be different between the two unique numbers). You can isolate the rightmost set bit using xor & (-xor).
  3. Partition the array into two groups based on the set bit. XOR all numbers in each group to find the two unique numbers.

Let’s implement this solution in PHP: 260. Single Number III

<?php
// Example usage:
$nums1 = [1, 2, 1, 3, 2, 5];
$result1 = singleNumber($nums1);
print_r($result1);

$nums2 = [-1, 0];
$result2 = singleNumber($nums2);
print_r($result2);

$nums3 = [0, 1];
$result3 = singleNumber($nums3);
print_r($result3);
?>

Explanation:

  • Step 1: The XOR operation eliminates the numbers that appear twice and results in xor being the XOR of the two unique numbers.
  • Step 2: The rightmost set bit is found, which helps in distinguishing the two unique numbers.
  • Step 3: The array is partitioned into two groups: one with the set bit and one without. XORing the numbers in each group gives the two unique numbers.

This solution runs in O(n) time and uses O(1) extra space, meeting the problem’s requirements.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

If you need any support regarding your website

If you like our blog, Please support us to improve

Buy Me a Coffee

Leave a Reply

Your email address will not be published. Required fields are marked *

RECENT POST
Leetcode Solutions

633. Sum of Square Numbers

Sum of Square Numbers Difficulty: Medium Topics: Math, Two Pointers, Binary Search Given a non-negative integer c, decide whether there’re

Leetcode Solutions

624. Maximum Distance in Arrays

Maximum Distance in Arrays Difficulty: Medium Topics: Array, Greedy You are given m arrays, where each array is sorted in