Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Single Number III problem solution

YASH PAL, 31 July 202420 January 2026

In this Leetcode Single Number III problem solution, we have 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.

Leetcode Single Number III problem solution

Leetcode Single Number III problem solution in Python.

def singleNumber(nums):
    a = set(nums)
    b = a.copy()
    for n in nums:
        if n in a:
            a.remove(n)
            continue
        if n in b: b.remove(n)
    return list(b)

Single Number III problem solution in Java.

class Solution {
    public int[] singleNumber(int[] nums) {
        int a = 0;
        int b = 0;
        int xor = 0;
        
        //get all xor
        for(int n : nums) xor = xor ^ n;
        
        //discover any set bit on the result, I'm using the first one
        int firstSetBit = 0;
        while(firstSetBit < 32) if ( ((xor>>firstSetBit)&1) == 1 ) break; else firstSetBit++;
        
        //put numbers with the corresponding bit set to zero into one variable
        //and if it is set to one, put it into the other variable
        for(int n : nums) {
            if ( ((n>>firstSetBit)&1) == 0 ) a ^= n;
            else b ^= n;
        }
        
        return new int[]{a,b};
    }
}

Problem solution in C++.

public:
    vector singleNumber(vector& nums) {
        vector op;
        long long int xorpair=0;
        for(auto &i:nums)
        {
            xorpair^=i;
        }
        long long int rsbm=xorpair&(-xorpair);
        long long int on=0;
        long long int off=0;
        for(auto &i:nums)
        {
            if(i&rsbm)
                on^=i;
            else
                off^=i;
        }
        op.push_back((int)(on^xorpair));
        op.push_back((int)(off^xorpair));
        return op;
        
    }
};

Problem solution in C.

int* singleNumber(int* nums, int numsSize, int* returnSize) {
    int i, *ret = calloc(*returnSize = 2, sizeof(int));
    for(i = 0; i < numsSize; ret[0] ^= nums[i++]);
    for(i = 0; i < numsSize; i++)
        if(nums[i] & ret[0] & -ret[0])
            ret[1] ^= nums[i];
    ret[0] ^= ret[1];
    return ret;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

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

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes