Single Number LeetCode Solution

Last updated on July 16th, 2024 at 04:18 am

Here, We see Single Number problem Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

List of all LeetCode Solution

Topics

Bit-Manipulation, Hash Table

Companies

Airbnb, Palantir

Level of Question

Easy

Single Number LeetCode Solution

Single Number LeetCode Solution

Problem Statement

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

Example 1:
Input: nums = [2,2,1]
Output: 1

Example 2:
Input: nums = [4,1,2,1,2]
Output: 4

Example 3:
Input: nums = [1]
Output: 1

1. Single Number Leetcode Solution C++

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        for(int i=1;i<nums.size();i+=2)
        {
            if(nums[i]!=nums[i-1])
                return nums[i-1];
        }
        return nums[nums.size()-1];
    }
};

1.1 Explanation

  • Sort the Array: The array nums is sorted in ascending order.
  • Iterate Through Sorted Array: Iterate through the sorted array in steps of 2. For each pair of elements, check if they are equal. If a pair of elements is not equal, return the first element of that pair (the single number).
  • Return the Last Element: If no single element is found in the loop, the single number is the last element in the array.

1.2 Time Complexity

  • O(n log n).

1.3 Space Complexity

  • O(1).

2. Single Number Leetcode Solution Java

class Solution {
    public int singleNumber(int[] nums) {
        int res = 0;
    for (int i = 0; i < nums.length; i++) {
        res = res^nums[i];
    }
    return res;
    }
}

2.1 Explanation

  • Initialize Result: Initialize a variable res to 0.
  • XOR Operation: Iterate through each element in the array and perform the XOR operation with res.
  • Return Result: The result will be the single number because the XOR of two identical numbers is 0, and the XOR of a number with 0 is the number itself.

2.2 Time Complexity

  • O(n).

2.3 Space Complexity

  • O(1).

3. Single Number Leetcode Solution JavaScript

var singleNumber = function(nums) {
    let uniqNum = 0;
    for (let idx = 0; idx < nums.length; idx++) {
        uniqNum = uniqNum ^ nums[idx];
    } return uniqNum;  
};

3.1 Explanation

  • Initialize Unique Number: Initialize a variable uniqNum to 0.
  • XOR Operation: Iterate through each element in the array and perform the XOR operation with uniqNum.
  • Return Unique Number: The result will be the single number for the same reason as explained in the Java code.

3.2 Time Complexity

O(n).

3.3 Space Complexity

O(1).

4. Single Number Solution Python

class Solution(object):
    def singleNumber(self, nums):
        return reduce(operator.xor, nums)

4.1 Explanation

  • Reduce Function with XOR:
  1. The reduce function applies the XOR operation cumulatively to the elements of the array nums.
  2. operator.xor is a function that performs the XOR operation between two numbers.

4.2 Time Complexity

  • O(n).

4.3 Space Complexity

  • O(1).
Scroll to Top