No result illustrationNo result illustration

DevLoom

search

Lets Connect

chevron right
Menu
Home

Home

Community

Community

Blog Posts

Blog Posts

Programming

Programming

Tech Topics

Tech Topics

Explore

Explore

Find Jobs

Find Jobs

Tags

Tags

DevLoomPerfect Place for devs
    profile

    Jubair

    upvoteupvote

    1

    downvote

    0

    star

    Single Number

    clock icon

    Asked 9 months ago

    message

    0Comments

    eye

    1 Views

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

    In Simple Words ! The problem "Single Number" asks us to find the element that appears only once in an array where every other element appears twice.

    Let's break down the problem and explore the three approaches

    Approach 1: Brute Force

     
    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            int num = 0;
            for (int i = 0; i < nums.size(); i++) {
                bool found = false;
                for (int j = 0; j < nums.size(); j++) {
                    if (i != j && nums[i] == nums[j]) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    num = nums[i];
                    break;
                }
            }
            return num;
        }
    };
     
    Explanation:
    1. Iterate through each element in the array.
    2. For each element, check if it appears again in the array (excluding itself).
    3. If no duplicate is found, mark the element as the single number.
    Time Complexity: O(n^2)
    Space Complexity: O(1)
     
    Approach 2: Hash Table
     
    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            unordered_map<int, int> hashTable;
            for (int num : nums) {
                hashTable[num]++;
            }
            for (auto& pair : hashTable) {
                if (pair.second == 1) {
                    return pair.first;
                }
            }
            return -1; // Should not reach here
        }
    };
    Explanation:
    1. Create a hash table to store the frequency of each number.
    2. Iterate through the array and increment the frequency of each number.
    3. Iterate through the hash table and find the number with frequency 1.
    Time Complexity: O(n)
    Space Complexity: O(n)
     
    Approach 3: XOR
    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            unordered_map<int, int> hashTable;
            for (int num : nums) {
                hashTable[num]++;
            }
            for (auto& pair : hashTable) {
                if (pair.second == 1) {
                    return pair.first;
                }
            }
            return -1; // Should not reach here
        }
    };
     
    Explanation:
     
    1. Initialize ans to 0.
    2. Iterate through the array and XOR each number with ans.
    3. The XOR operation has the following properties:
      • a ^ a = 0 (self-inverse)
      • a ^ 0 = a (identity)
      • a ^ b = b ^ a (commutative)
      • (a ^ b) ^ c = a ^ (b ^ c) (associative)
    Since every number appears twice except for one, the XOR operation will cancel out all duplicate numbers, leaving only the single number.
    Time Complexity: O(n)
    Space Complexity: O(1)
    The XOR approach is the most efficient solution, taking advantage of the properties of the XOR operation to find the single number in linear time and constant space.
    Atlassian
    Math
    easy

    Share

    Write your comment here !

    0 Responses