Open In App

Find the two numbers with odd occurrences in an unsorted array

Last Updated : 10 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an unsorted array that contains even number of occurrences for all numbers except two numbers. The task is to find the two numbers which have odd occurrences in O(n) time complexity and O(1) extra space.

Examples: 

Input: {12, 23, 34, 12, 12, 23, 12, 45}
Output: 34 45

Input: {4, 4, 100, 5000, 4, 4, 4, 4, 100, 100}
Output: 100 5000

Input: {10, 20}
Output: 10 20

[Naive Approach] - O(n^2) time and O(1) space

The idea is to run two nested loops. The outer loop picks an element and the inner loop counts the number of occurrences of the picked element. If the count of occurrences is odd, then append the element to result, while ensuring that one element is only pushed once into result.

C++
// C++ program to find the two numbers with 
// odd occurrences in an unsorted array
#include <bits/stdc++.h>
using namespace std;

// Function to find the two elements
// with odd occurrences.
vector<int> twoOddNum(vector<int>& arr) {
    int n = arr.size();
    
    vector<int> ans = {-1, -1};
    int index = 0;
    
    // Check for each element
    for (int i=0; i<n; i++) {
        
        // Get the count of arr[i]
        int cnt = 0;
        for (int j=0; j<n; j++) {
            if (arr[j]==arr[i]) cnt++;
        }
        
        // If cnt is odd and arr[i]
        // has not been added to result yet.
        if (cnt%2==1 && ans[0]!=arr[i] && ans[1]!=arr[i]) {
            ans[index++] = arr[i];
        }
    }
    
    // Return in decreasing order
    if (ans[0]<ans[1]) swap(ans[0], ans[1]);
    
    return ans;
}

int main() {
    vector<int> arr = {12, 23, 34, 12, 12, 23, 12, 45};
    vector<int> ans = twoOddNum(arr);
    cout<<ans[0]<<" "<<ans[1]<<endl;
    return 0;
}
Java
// Java program to find the two numbers with 
// odd occurrences in an unsorted array
import java.util.*;

class GfG {

    // Function to find the two elements
    // with odd occurrences.
    static int[] twoOddNum(int[] arr) {
        int n = arr.length;
        
        int[] ans = {-1, -1};
        int index = 0;
        
        // Check for each element
        for (int i = 0; i < n; i++) {
            
            // Get the count of arr[i]
            int cnt = 0;
            for (int j = 0; j < n; j++) {
                if (arr[j] == arr[i]) cnt++;
            }
            
            // If cnt is odd and arr[i]
            // has not been added to result yet.
            if (cnt % 2 == 1 && ans[0] != arr[i] && ans[1] != arr[i]) {
                ans[index++] = arr[i];
            }
        }
        
        // Return in decreasing order
        if (ans[0] < ans[1]) {
            int temp = ans[0];
            ans[0] = ans[1];
            ans[1] = temp;
        }
        
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
        int[] ans = twoOddNum(arr);
        System.out.println(ans[0] + " " + ans[1]);
    }
}
Python
# Python program to find the two numbers with 
# odd occurrences in an unsorted array

# Function to find the two elements
# with odd occurrences.
def twoOddNum(arr):
    n = len(arr)
    
    ans = [-1, -1]
    index = 0
    
    # Check for each element
    for i in range(n):
        
        # Get the count of arr[i]
        cnt = 0
        for j in range(n):
            if arr[j] == arr[i]:
                cnt += 1
        
        # If cnt is odd and arr[i]
        # has not been added to result yet.
        if cnt % 2 == 1 and ans[0] != arr[i] and ans[1] != arr[i]:
            ans[index] = arr[i]
            index += 1
    
    # Return in decreasing order
    if ans[0] < ans[1]:
        ans[0], ans[1] = ans[1], ans[0]
    
    return ans

if __name__ == "__main__":
    arr = [12, 23, 34, 12, 12, 23, 12, 45]
    ans = twoOddNum(arr)
    print(ans[0], ans[1])
C#
// C# program to find the two numbers with 
// odd occurrences in an unsorted array
using System;

class GfG {

    // Function to find the two elements
    // with odd occurrences.
    static int[] twoOddNum(int[] arr) {
        int n = arr.Length;
        
        int[] ans = {-1, -1};
        int index = 0;
        
        // Check for each element
        for (int i = 0; i < n; i++) {
            
            // Get the count of arr[i]
            int cnt = 0;
            for (int j = 0; j < n; j++) {
                if (arr[j] == arr[i]) cnt++;
            }
            
            // If cnt is odd and arr[i]
            // has not been added to result yet.
            if (cnt % 2 == 1 && ans[0] != arr[i] && ans[1] != arr[i]) {
                ans[index++] = arr[i];
            }
        }
        
        // Return in decreasing order
        if (ans[0] < ans[1]) {
            int temp = ans[0];
            ans[0] = ans[1];
            ans[1] = temp;
        }
        
        return ans;
    }

    static void Main(string[] args) {
        int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
        int[] ans = twoOddNum(arr);
        Console.WriteLine(ans[0] + " " + ans[1]);
    }
}
JavaScript
// JavaScript program to find the two numbers with 
// odd occurrences in an unsorted array

// Function to find the two elements
// with odd occurrences.
function twoOddNum(arr) {
    let n = arr.length;
    
    let ans = [-1, -1];
    let index = 0;
    
    // Check for each element
    for (let i = 0; i < n; i++) {
        
        // Get the count of arr[i]
        let cnt = 0;
        for (let j = 0; j < n; j++) {
            if (arr[j] == arr[i]) cnt++;
        }
        
        // If cnt is odd and arr[i]
        // has not been added to result yet.
        if (cnt % 2 == 1 && ans[0] != arr[i] && ans[1] != arr[i]) {
            ans[index++] = arr[i];
        }
    }
    
    // Return in decreasing order
    if (ans[0] < ans[1]) {
        [ans[0], ans[1]] = [ans[1], ans[0]];
    }
    
    return ans;
}

let arr = [12, 23, 34, 12, 12, 23, 12, 45];
let ans = twoOddNum(arr);
console.log(ans[0], ans[1]);

Output
45 34

[Better Approach - 1] Using Sorting - O(n logn) time and O(1) space

The idea is to use sorting and then find the frequency of each element in linear time.

C++
// C++ program to find the two numbers with 
// odd occurrences in an unsorted array
#include <bits/stdc++.h>
using namespace std;

// Function to find the two elements
// with odd occurrences.
vector<int> twoOddNum(vector<int>& arr) {
    int n = arr.size();
    
    vector<int> ans = {-1, -1};
    int index = 0;
    
    // Sort the array 
    sort(arr.begin(), arr.end());
    
    // Get count of each element 
    int i = 0;
    while (i<n) {
        int val = arr[i];
        int cnt = 0;
        while (i<n && arr[i]==val) {
            cnt++;
            i++;
        }
        
        // If count is odd 
        if (cnt%2==1) ans[index++] = val;
    }
    
    // Return in decreasing order
    if (ans[0]<ans[1]) swap(ans[0], ans[1]);
    
    return ans;
}

int main() {
    vector<int> arr = {12, 23, 34, 12, 12, 23, 12, 45};
    vector<int> ans = twoOddNum(arr);
    cout<<ans[0]<<" "<<ans[1]<<endl;
    return 0;
}
Java
// Java program to find the two numbers with 
// odd occurrences in an unsorted array
import java.util.Arrays;

class GfG {

    // Function to find the two elements
    // with odd occurrences.
    static int[] twoOddNum(int[] arr) {
        int n = arr.length;
        
        int[] ans = {-1, -1};
        int index = 0;
        
        // Sort the array 
        Arrays.sort(arr);
        
        // Get count of each element 
        int i = 0;
        while (i < n) {
            int val = arr[i];
            int cnt = 0;
            while (i < n && arr[i] == val) {
                cnt++;
                i++;
            }
            
            // If count is odd 
            if (cnt % 2 == 1) ans[index++] = val;
        }
        
        // Return in decreasing order
        if (ans[0] < ans[1]) {
            int temp = ans[0];
            ans[0] = ans[1];
            ans[1] = temp;
        }
        
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
        int[] ans = twoOddNum(arr);
        System.out.println(ans[0] + " " + ans[1]);
    }
}
Python
# Python program to find the two numbers with 
# odd occurrences in an unsorted array

# Function to find the two elements
# with odd occurrences.
def twoOddNum(arr):
    n = len(arr)
    
    ans = [-1, -1]
    index = 0
    
    # Sort the array 
    arr.sort()
    
    # Get count of each element 
    i = 0
    while i < n:
        val = arr[i]
        cnt = 0
        while i < n and arr[i] == val:
            cnt += 1
            i += 1
        
        # If count is odd 
        if cnt % 2 == 1:
            ans[index] = val
            index += 1
    
    # Return in decreasing order
    if ans[0] < ans[1]:
        ans[0], ans[1] = ans[1], ans[0]
    
    return ans

if __name__ == "__main__":
    arr = [12, 23, 34, 12, 12, 23, 12, 45]
    ans = twoOddNum(arr)
    print(ans[0], ans[1])
C#
// C# program to find the two numbers with 
// odd occurrences in an unsorted array
using System;

class GfG {

    // Function to find the two elements
    // with odd occurrences.
    static int[] twoOddNum(int[] arr) {
        int n = arr.Length;
        
        int[] ans = {-1, -1};
        int index = 0;
        
        // Sort the array 
        Array.Sort(arr);
        
        // Get count of each element 
        int i = 0;
        while (i < n) {
            int val = arr[i];
            int cnt = 0;
            while (i < n && arr[i] == val) {
                cnt++;
                i++;
            }
            
            // If count is odd 
            if (cnt % 2 == 1) ans[index++] = val;
        }
        
        // Return in decreasing order
        if (ans[0] < ans[1]) {
            int temp = ans[0];
            ans[0] = ans[1];
            ans[1] = temp;
        }
        
        return ans;
    }

    static void Main(string[] args) {
        int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
        int[] ans = twoOddNum(arr);
        Console.WriteLine(ans[0] + " " + ans[1]);
    }
}
JavaScript
// JavaScript program to find the two numbers with 
// odd occurrences in an unsorted array

// Function to find the two elements
// with odd occurrences.
function twoOddNum(arr) {
    let n = arr.length;
    
    let ans = [-1, -1];
    let index = 0;
    
    // Sort the array 
    arr.sort((a, b) => a - b);
    
    // Get count of each element 
    let i = 0;
    while (i < n) {
        let val = arr[i];
        let cnt = 0;
        while (i < n && arr[i] === val) {
            cnt++;
            i++;
        }
        
        // If count is odd 
        if (cnt % 2 === 1) {
            ans[index++] = val;
        }
    }
    
    // Return in decreasing order
    if (ans[0] < ans[1]) {
        [ans[0], ans[1]] = [ans[1], ans[0]];
    }
    
    return ans;
}

let arr = [12, 23, 34, 12, 12, 23, 12, 45];
let ans = twoOddNum(arr);
console.log(ans[0], ans[1]);

Output
45 34

[Better Approach - 2] Using Hash Map - O(n) time and O(n) space

The idea is to use a hash map to store the count of all values. Then iterate through the map and return the values with odd count.

Step by step approach:

  1. Traverse all elements and insert them in to a hash table. Element is used as key and the frequency is used as the value in the hash table. 
  2. Iterate through the map and append the values with odd count.
C++
// C++ program to find the two numbers with 
// odd occurrences in an unsorted array
#include <bits/stdc++.h>
using namespace std;

// Function to find the two elements
// with odd occurrences.
vector<int> twoOddNum(vector<int>& arr) {
    int n = arr.size();
    
    vector<int> ans = {-1, -1};
    int index = 0;
    
    // Map to store count
    unordered_map<int,int> cnt;
    
    // Count occurrences in array 
    for (int i=0; i<n; i++) {
        cnt[arr[i]]++;
    }
    
    // Append the values with odd 
    // count to result 
    for (auto p: cnt) {
        if (p.second%2==1) {
            ans[index++] = p.first;
        }
    }
    
    // Return in decreasing order
    if (ans[0]<ans[1]) swap(ans[0], ans[1]);
    
    return ans;
}

int main() {
    vector<int> arr = {12, 23, 34, 12, 12, 23, 12, 45};
    vector<int> ans = twoOddNum(arr);
    cout<<ans[0]<<" "<<ans[1]<<endl;
    return 0;
}
Java
// Java program to find the two numbers with 
// odd occurrences in an unsorted array
import java.util.HashMap;

class GfG {

    // Function to find the two elements
    // with odd occurrences.
    static int[] twoOddNum(int[] arr) {
        int n = arr.length;
        
        int[] ans = {-1, -1};
        int index = 0;
        
        // Map to store count
        HashMap<Integer, Integer> cnt = new HashMap<>();
        
        // Count occurrences in array 
        for (int i = 0; i < n; i++) {
            cnt.put(arr[i], cnt.getOrDefault(arr[i], 0) + 1);
        }
        
        // Append the values with odd 
        // count to result 
        for (var entry : cnt.entrySet()) {
            if (entry.getValue() % 2 == 1) {
                ans[index++] = entry.getKey();
            }
        }
        
        // Return in decreasing order
        if (ans[0] < ans[1]) {
            int temp = ans[0];
            ans[0] = ans[1];
            ans[1] = temp;
        }
        
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
        int[] ans = twoOddNum(arr);
        System.out.println(ans[0] + " " + ans[1]);
    }
}
Python
# Python program to find the two numbers with 
# odd occurrences in an unsorted array

# Function to find the two elements
# with odd occurrences.
def twoOddNum(arr):
    n = len(arr)
    
    ans = [-1, -1]
    index = 0
    
    # Map to store count
    cnt = {}
    
    # Count occurrences in array 
    for num in arr:
        cnt[num] = cnt.get(num, 0) + 1
    
    # Append the values with odd 
    # count to result 
    for key, value in cnt.items():
        if value % 2 == 1:
            ans[index] = key
            index += 1
    
    # Return in decreasing order
    if ans[0] < ans[1]:
        ans[0], ans[1] = ans[1], ans[0]
    
    return ans

if __name__ == "__main__":
    arr = [12, 23, 34, 12, 12, 23, 12, 45]
    ans = twoOddNum(arr)
    print(ans[0], ans[1])
C#
// C# program to find the two numbers with 
// odd occurrences in an unsorted array
using System;
using System.Collections.Generic;

class GfG {

    // Function to find the two elements
    // with odd occurrences.
    static int[] twoOddNum(int[] arr) {
        int n = arr.Length;
        
        int[] ans = {-1, -1};
        int index = 0;
        
        // Map to store count
        Dictionary<int, int> cnt = new Dictionary<int, int>();
        
        // Count occurrences in array 
        for (int i = 0; i < n; i++) {
            if (cnt.ContainsKey(arr[i])) {
                cnt[arr[i]]++;
            } else {
                cnt[arr[i]] = 1;
            }
        }
        
        // Append the values with odd 
        // count to result 
        foreach (var entry in cnt) {
            if (entry.Value % 2 == 1) {
                ans[index++] = entry.Key;
            }
        }
        
        // Return in decreasing order
        if (ans[0] < ans[1]) {
            int temp = ans[0];
            ans[0] = ans[1];
            ans[1] = temp;
        }
        
        return ans;
    }

    static void Main(string[] args) {
        int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
        int[] ans = twoOddNum(arr);
        Console.WriteLine(ans[0] + " " + ans[1]);
    }
}
JavaScript
// JavaScript program to find the two numbers with 
// odd occurrences in an unsorted array

// Function to find the two elements
// with odd occurrences.
function twoOddNum(arr) {
    let n = arr.length;
    
    let ans = [-1, -1];
    let index = 0;
    
    // Map to store count
    let cnt = new Map();
    
    // Count occurrences in array 
    for (let i = 0; i < n; i++) {
        cnt.set(arr[i], (cnt.get(arr[i]) || 0) + 1);
    }
    
    // Append the values with odd 
    // count to result 
    for (let [key, value] of cnt) {
        if (value % 2 === 1) {
            ans[index++] = key;
        }
    }
    
    // Return in decreasing order
    if (ans[0] < ans[1]) {
        [ans[0], ans[1]] = [ans[1], ans[0]];
    }
    
    return ans;
}

let arr = [12, 23, 34, 12, 12, 23, 12, 45];
let ans = twoOddNum(arr);
console.log(ans[0], ans[1]);

Output
45 34

[Expected Approach] Using Bit Manipulation - O(n) time and O(1) space

The idea is to first compute the XOR of all array elements, which results in xorVal = x^y (where x and y are our target numbers) since even count elements cancel each other out (as a^a = 0).

Now we have value of x^y, but we need to find the values of individual elements x and y. To find these values, we divide array elements into two sets.

We find the rightmost set bit in xorVal, which indicates a position where x and y differ (one has 1 and other has 0). Using this bit position as a filter, we partition numbers into two sets - one set where this bit is set and another where it's not. This separation ensures that x and y go into different sets.

Finally, we XOR all numbers in each set separately; even count numbers cancel out again, leaving us with one unique number in each set, which we return in decreasing order.

Step by step approach:

  1.  The first step is to do XOR of all elements present in array (lets say xorVal). XOR of all elements gives us XOR of x and y because of the following properties of XOR operation:
    • XOR of any number n with itself gives us 0, i.e., n ^ n = 0 
    • XOR of any number n with 0 gives us n, i.e., n ^ 0 = n 
    • XOR is cumulative and associative.
  2. Pick a set bit in xorVal, which indicates a position where x and y differ (one has 1 and other has 0). So we separate x and y into two different groups, along with rest of the numbers of list, based on whether the number has same set-bit or not.
    •  We will choose right most bit in this case. We can get right most set bit by performing AND operation of xorVal with its negative counterpart (xorVal & -xorVal).
  3. In third step, we separate x and y into two different groups. We now know that for selected set bit index, x and y have different corresponding bits. If we AND all numbers in list with set bit, some will give 0 and others will give 1. We will put all numbers giving zeroes in one group and ones in another.
  4. Return the result of the two groups in decreasing order.

Illustration:

Taking example of arr = [4, 2, 4, 10, 2, 3, 3, 12]

  1. XOR of all values in the array will be equal to
    • 4 ^ 2 ^ 4 ^ 10 ^ 2 ^ 3 ^ 3 ^ 12
    • (4 ^ 4) ^ (2 ^ 2) ^ 10 ^ (3 ^ 3) ^ 12
    • 0 ^ 0 ^ 10 ^ 0 ^ 12
    • 10 ^ 12
    • 6
  2. Find right most set bit of xorVal:
    • 6 & (-6)
    • 0110 & 1010 (Binary representation)
    • 0010
    • 2
  3. Separate all values on basis of set bit and find their xor value:
    • Values with set bit
      • 2 ^ 10 ^ 2 ^ 3 ^ 3
      • 10
    • Values with unset bit
      • 4 ^ 4 ^ 12
      • 12
  4. Return the result in decreasing order, i.e., {12, 10}.

Below is the implementation of the above approach:

C++
// C++ program to find the two numbers with 
// odd occurrences in an unsorted array
#include <bits/stdc++.h>
using namespace std;

// Function to find the two elements
// with odd occurrences.
vector<int> twoOddNum(vector<int>& arr) {
    int n = arr.size();
    
    // Get the XOR of the two numbers we need to find
    int xorVal = 0;
    for (int i=0; i<n; i++) {
        xorVal = arr[i] ^ xorVal;
    }

    // Get its last set bit
    xorVal &= -xorVal;

    vector<int> ans(2, 0);
    
    for (int i=0; i<n; i++) {
        int num = arr[i];
        
        // If bit is not set, it 
        // belongs to first set.
        if ((num & xorVal) == 0) { 
            ans[0] ^= num;
        }
        
        // If bit is set, it 
        // belongs to 2nd set.
        else { 
            ans[1] ^= num;
        }
    }
    
    // Return in decreasing order
    if (ans[0]<ans[1]) swap(ans[0], ans[1]);
    
    return ans;
}

int main() {
    vector<int> arr = {12, 23, 34, 12, 12, 23, 12, 45};
    vector<int> ans = twoOddNum(arr);
    cout<<ans[0]<<" "<<ans[1]<<endl;
    return 0;
}
Java
// Java program to find the two numbers with 
// odd occurrences in an unsorted array

import java.util.*;

class GfG {

    // Function to find the two elements
    // with odd occurrences.
    static int[] twoOddNum(int[] arr) {
        int n = arr.length;
        
        // Get the XOR of the two numbers we need to find
        int xorVal = 0;
        for (int i = 0; i < n; i++) {
            xorVal = arr[i] ^ xorVal;
        }

        // Get its last set bit
        xorVal &= -xorVal;

        int[] ans = {0, 0};
        
        for (int i = 0; i < n; i++) {
            int num = arr[i];
            
            // If bit is not set, it 
            // belongs to first set.
            if ((num & xorVal) == 0) { 
                ans[0] ^= num;
            }
            
            // If bit is set, it 
            // belongs to 2nd set.
            else { 
                ans[1] ^= num;
            }
        }
        
        // Return in decreasing order
        if (ans[0] < ans[1]) {
            int temp = ans[0];
            ans[0] = ans[1];
            ans[1] = temp;
        }
        
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
        int[] ans = twoOddNum(arr);
        System.out.println(ans[0] + " " + ans[1]);
    }
}
Python
# Python program to find the two numbers with 
# odd occurrences in an unsorted array

# Function to find the two elements
# with odd occurrences.
def twoOddNum(arr):
    n = len(arr)
    
    # Get the XOR of the two numbers we need to find
    xorVal = 0
    for num in arr:
        xorVal ^= num

    # Get its last set bit
    xorVal &= -xorVal

    ans = [0, 0]
    
    for num in arr:
      
        # If bit is not set, it 
        # belongs to first set.
        if (num & xorVal) == 0:
            ans[0] ^= num
            
        # If bit is set, it 
        # belongs to 2nd set.
        else:
            ans[1] ^= num
    
    # Return in decreasing order
    if ans[0] < ans[1]:
        ans[0], ans[1] = ans[1], ans[0]
    
    return ans

if __name__ == "__main__":
    arr = [12, 23, 34, 12, 12, 23, 12, 45]
    ans = twoOddNum(arr)
    print(ans[0], ans[1])
C#
// C# program to find the two numbers with 
// odd occurrences in an unsorted array
using System;

class GfG {

    // Function to find the two elements
    // with odd occurrences.
    static int[] twoOddNum(int[] arr) {
        int n = arr.Length;
        
        // Get the XOR of the two numbers we need to find
        int xorVal = 0;
        for (int i = 0; i < n; i++) {
            xorVal = arr[i] ^ xorVal;
        }

        // Get its last set bit
        xorVal &= -xorVal;

        int[] ans = {0, 0};
        
        for (int i = 0; i < n; i++) {
            int num = arr[i];
            
            // If bit is not set, it 
            // belongs to first set.
            if ((num & xorVal) == 0) { 
                ans[0] ^= num;
            }
            
            // If bit is set, it 
            // belongs to 2nd set.
            else { 
                ans[1] ^= num;
            }
        }
        
        // Return in decreasing order
        if (ans[0] < ans[1]) {
            int temp = ans[0];
            ans[0] = ans[1];
            ans[1] = temp;
        }
        
        return ans;
    }

    static void Main(string[] args) {
        int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
        int[] ans = twoOddNum(arr);
        Console.WriteLine(ans[0] + " " + ans[1]);
    }
}
JavaScript
// JavaScript program to find the two numbers with 
// odd occurrences in an unsorted array

// Function to find the two elements
// with odd occurrences.
function twoOddNum(arr) {
    let n = arr.length;
    
    // Get the XOR of the two numbers we need to find
    let xorVal = 0;
    for (let i = 0; i < n; i++) {
        xorVal = arr[i] ^ xorVal;
    }

    // Get its last set bit
    xorVal &= -xorVal;

    let ans = [0, 0];
    
    for (let i = 0; i < n; i++) {
        let num = arr[i];
        
        // If bit is not set, it 
        // belongs to first set.
        if ((num & xorVal) === 0) {
            ans[0] ^= num;
        }
        
        // If bit is set, it 
        // belongs to 2nd set.
        else {
            ans[1] ^= num;
        }
    }
    
    // Return in decreasing order
    if (ans[0] < ans[1]) {
        [ans[0], ans[1]] = [ans[1], ans[0]];
    }
    
    return ans;
}

let arr = [12, 23, 34, 12, 12, 23, 12, 45];
let ans = twoOddNum(arr);
console.log(ans[0], ans[1]);

Output
45 34

Next Article
Article Tags :
Practice Tags :

Similar Reads