Mnewsfr.com

Non Repeating Numbers

Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.

Example -1

Input: N = 2 arr[] = {1, 2, 3, 2, 1, 4}

Output: 1 3 Explanation: 1 3 occur exactly once.

Example -2

Input: N = 1 arr[] = {2, 1, 3, 2}

Output: 1 3 Explanation: 1 3 occur exactly once.

Your Task :

You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.

Non Repeating Numbers In Python

def singleNumber(nums):
    xor_result = 0
    
    # Calculate the XOR of all numbers in the array
    for num in nums:
        xor_result ^= num
    
    # Find the rightmost set bit in the XOR result
    # This helps us partition the numbers into two groups
    rightmost_set_bit = 1
    while (xor_result & rightmost_set_bit) == 0:
        rightmost_set_bit <<= 1
    
    # Initialize two variables to hold the XOR results for each group
    group1 = 0
    group2 = 0
    
    # Partition the numbers into two groups based on the rightmost set bit
    for num in nums:
        if num & rightmost_set_bit:
            group1 ^= num
        else:
            group2 ^= num
    
    # Return the two distinct numbers in ascending order
    return [min(group1, group2), max(group1, group2)]

Non Repeating Numbers In C#

using System;
using System.Collections.Generic;

class Program
{
    static List<int> SingleNumber(int[] nums)
    {
        int xorResult = 0;

        // Calculate the XOR of all numbers in the array
        foreach (int num in nums)
        {
            xorResult ^= num;
        }

        // Find the rightmost set bit in the XOR result
        // This helps us partition the numbers into two groups
        int rightmostSetBit = 1;
        while ((xorResult & rightmostSetBit) == 0)
        {
            rightmostSetBit <<= 1;
        }

        // Initialize two variables to hold the XOR results for each group
        int group1 = 0;
        int group2 = 0;

        // Partition the numbers into two groups based on the rightmost set bit
        foreach (int num in nums)
        {
            if ((num & rightmostSetBit) != 0)
            {
                group1 ^= num;
            }
            else
            {
                group2 ^= num;
            }
        }

        // Return the two distinct numbers in ascending order
        return new List<int> { Math.Min(group1, group2), Math.Max(group1, group2) };
    }

    static void Main(string[] args)
    {
        int[] nums = { 1, 2, 3, 2, 1, 4 };
        List<int> result = SingleNumber(nums);
        Console.WriteLine(string.Join(", ", result));  // Output: 3, 4
    }
}

Expected Time Complexity - O(N)

Expected Auxiliary Space - O(1)

Constraints

1 <= length of array <= 106 1 <= Elements in array <= 5 * 106