Find Unique Pair in Array

Last Updated : 30 Jul, 2026

Given an array arr[] in which every element appears exactly twice except for two elements that appear only once, find those two unique elements and return them in sorted order.

Examples:

Input: arr[] = [2, 2, 5, 5, 6, 7]
Output: [6, 7]
Explanation: 2 and 5 appear twice, while 6 and 7 appear only once.

Input: arr[] = [1, 3, 4, 1]
Output: [3, 4]
Explanation: 1 appears twice, while 3 and 4 appear only once.

Try It Yourself
redirect icon

[Naive Approach] Count Frequency for Each Element - O(n^2) Time and O(1) Space

The idea is to check every element and count how many times it appears in the array. The two elements with frequency 1 are the required unique pair.

C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> findUniquePair(vector<int>& arr) {
    vector<int> ans;

    for (int i = 0; i < arr.size(); i++) {
        int count = 0;

        // Count frequency of current element.
        for (int j = 0; j < arr.size(); j++) {
            if (arr[i] == arr[j]) count++;
        }

        if (count == 1) {
            ans.push_back(arr[i]);

            // Stop after finding both unique elements.
            if (ans.size() == 2) break;
        }
    }

    sort(ans.begin(), ans.end());
    return ans;
}

int main() {
    vector<int> arr = {2, 2, 5, 5, 6, 7};
    vector<int> ans = findUniquePair(arr);
    cout << ans[0] << " " << ans[1] << endl;

    arr = {1, 3, 4, 1};
    ans = findUniquePair(arr);
    cout << ans[0] << " " << ans[1] << endl;

    return 0;
}
Java
import java.util.ArrayList;
import java.util.Collections;

class GFG {
    static ArrayList<Integer> findUniquePair(int[] arr) {
        ArrayList<Integer> ans = new ArrayList<>();

        for (int i = 0; i < arr.length; i++) {
            int count = 0;

            // Count frequency of current element.
            for (int j = 0; j < arr.length; j++) {
                if (arr[i] == arr[j]) count++;
            }

            if (count == 1) {
                ans.add(arr[i]);
            
                // Stop after finding both unique elements.
                if (ans.size() == 2) {
                    break;
                }
            }
        }

        Collections.sort(ans);
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {2, 2, 5, 5, 6, 7};
        System.out.println(findUniquePair(arr));

        arr = new int[]{1, 3, 4, 1};
        System.out.println(findUniquePair(arr));
    }
}
Python
def findUniquePair(arr):
    ans = []

    for x in arr:
        count = 0

        # Count frequency of current element.
        for y in arr:
            if x == y:
                count += 1

        if count == 1:
            ans.append(x)
        
            # Stop after finding both unique elements.
            if len(ans) == 2:
                break

    ans.sort()
    return ans


if __name__ == "__main__":
    print(findUniquePair([2, 2, 5, 5, 6, 7]))
    print(findUniquePair([1, 3, 4, 1]))
C#
using System;
using System.Collections.Generic;

class GFG {
    static List<int> findUniquePair(int[] arr) {
        List<int> ans = new List<int>();

        for (int i = 0; i < arr.Length; i++) {
            int count = 0;

            // Count frequency of current element.
            for (int j = 0; j < arr.Length; j++) {
                if (arr[i] == arr[j]) count++;
            }

            if (count == 1) {
                ans.Add(arr[i]);
            
                // Stop after finding both unique elements.
                if (ans.Count == 2) {
                    break;
                }
            }
        }

        ans.Sort();
        return ans;
    }

    static void Main() {
        Console.WriteLine(string.Join(" ", findUniquePair(new int[] {2, 2, 5, 5, 6, 7})));
        Console.WriteLine(string.Join(" ", findUniquePair(new int[] {1, 3, 4, 1})));
    }
}
JavaScript
function findUniquePair(arr) {
    const ans = [];

    for (const x of arr) {
        let count = 0;

        // Count frequency of current element.
        for (const y of arr) {
            if (x === y) count++;
        }

        if (count === 1) {
            ans.push(x);
        
            // Stop after finding both unique elements.
            if (ans.length === 2) {
                break;
            }
        }
    }

    ans.sort((a, b) => a - b);
    return ans;
}

// Driver Code
console.log(findUniquePair([2, 2, 5, 5, 6, 7]).join(" "));
console.log(findUniquePair([1, 3, 4, 1]).join(" "));

Output
6 7
3 4

[Better Approach] Using Sorting - O(n log n) Time and O(1) Space

The idea is to sort the array so equal elements come together. Then traverse the array and find elements that do not have the same adjacent element. These two elements are the required unique pair.

C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> findUniquePair(vector<int>& arr) {
    sort(arr.begin(), arr.end());
    vector<int> ans;

    for (int i = 0; i < arr.size(); i++) {
        
        // Skip paired elements.
        if (i + 1 < arr.size() && arr[i] == arr[i + 1]) {
            i++;
        } else {
            ans.push_back(arr[i]);
        }
    }

    return ans;
}

int main() {
    vector<int> arr = {2, 2, 5, 5, 6, 7};
    vector<int> ans = findUniquePair(arr);
    cout << ans[0] << " " << ans[1] << endl;

    arr = {1, 3, 4, 1};
    ans = findUniquePair(arr);
    cout << ans[0] << " " << ans[1] << endl;

    return 0;
}
Java
import java.util.ArrayList;
import java.util.Arrays;

class GFG {
    static ArrayList<Integer> findUniquePair(int[] arr) {
        Arrays.sort(arr);
        ArrayList<Integer> ans = new ArrayList<>();

        for (int i = 0; i < arr.length; i++) {
            
            // Skip paired elements.
            if (i + 1 < arr.length && arr[i] == arr[i + 1]) {
                i++;
            } else {
                ans.add(arr[i]);
            }
        }

        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {2, 2, 5, 5, 6, 7};
        System.out.println(findUniquePair(arr));

        arr = new int[]{1, 3, 4, 1};
        System.out.println(findUniquePair(arr));
    }
}
Python
def findUniquePair(arr):
    arr.sort()
    ans = []
    i = 0

    while i < len(arr):
        
        # Skip paired elements.
        if i + 1 < len(arr) and arr[i] == arr[i + 1]:
            i += 2
        else:
            ans.append(arr[i])
            i += 1

    return ans


if __name__ == "__main__":
    print(findUniquePair([2, 2, 5, 5, 6, 7]))
    print(findUniquePair([1, 3, 4, 1]))
C#
using System;
using System.Collections.Generic;

class GFG {
    static List<int> findUniquePair(int[] arr) {
        Array.Sort(arr);
        List<int> ans = new List<int>();

        for (int i = 0; i < arr.Length; i++) {
            
            // Skip paired elements.
            if (i + 1 < arr.Length && arr[i] == arr[i + 1]) {
                i++;
            } else {
                ans.Add(arr[i]);
            }
        }

        return ans;
    }

    static void Main() {
        Console.WriteLine(string.Join(" ", findUniquePair(new int[] {2, 2, 5, 5, 6, 7})));
        Console.WriteLine(string.Join(" ", findUniquePair(new int[] {1, 3, 4, 1})));
    }
}
JavaScript
function findUniquePair(arr) {
    arr.sort((a, b) => a - b);
    const ans = [];

    for (let i = 0; i < arr.length; i++) {
        
        // Skip paired elements.
        if (i + 1 < arr.length && arr[i] === arr[i + 1]) {
            i++;
        } else {
            ans.push(arr[i]);
        }
    }

    return ans;
}

// Driver Code
console.log(findUniquePair([2, 2, 5, 5, 6, 7]).join(" "));
console.log(findUniquePair([1, 3, 4, 1]).join(" "));

Output
6 7
3 4

[Expected Approach] XOR Partitioning - O(n) Time and O(1) Space

The idea is similar to Find Two Missing Numbers

The idea is to XOR all elements. Since paired elements cancel out, the result becomes XOR of the two unique elements. Pick any set bit from this XOR value and divide all numbers into two groups based on that bit. Each group contains one unique element, which can be found by XORing all elements in that group.

Let us understand with example:

For arr = [2, 2, 5, 5, 6, 7]:

  • xr = 2 ^ 2 ^ 5 ^ 5 ^ 6 ^ 7 = 1
  • Rightmost set bit of xr is 1.
  • Using this bit: Group 1 gives 7, Group 2 gives 6

Sorted answer is [6, 7].

C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> findUniquePair(vector<int>& arr) {
    int xr = 0;

    // XOR of all elements gives XOR of the two unique numbers.
    for (int x : arr) {
        xr ^= x;
    }

    int setBit = xr & -xr;
    int first = 0, second = 0;

    // Split elements into two groups using setBit.
    for (int x : arr) {
        if (x & setBit) {
            first ^= x;
        } else {
            second ^= x;
        }
    }

    if (first > second) swap(first, second);
    return {first, second};
}

int main() {
    vector<int> arr = {2, 2, 5, 5, 6, 7};
    vector<int> ans = findUniquePair(arr);
    cout << ans[0] << " " << ans[1] << endl;

    arr = {1, 3, 4, 1};
    ans = findUniquePair(arr);
    cout << ans[0] << " " << ans[1] << endl;

    return 0;
}
Java
import java.util.ArrayList;

class GFG {
    static ArrayList<Integer> findUniquePair(int[] arr) {
        int xr = 0;

        // XOR of all elements gives XOR of the two unique numbers.
        for (int x : arr) {
            xr ^= x;
        }

        int setBit = xr & -xr;
        int first = 0, second = 0;

        // Split elements into two groups using setBit.
        for (int x : arr) {
            if ((x & setBit) != 0) {
                first ^= x;
            } else {
                second ^= x;
            }
        }

        ArrayList<Integer> ans = new ArrayList<>();
        ans.add(Math.min(first, second));
        ans.add(Math.max(first, second));

        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {2, 2, 5, 5, 6, 7};
        System.out.println(findUniquePair(arr));

        arr = new int[]{1, 3, 4, 1};
        System.out.println(findUniquePair(arr));
    }
}
Python
def findUniquePair(arr):
    xr = 0

    # XOR of all elements gives XOR of the two unique numbers.
    for x in arr:
        xr ^= x

    set_bit = xr & -xr
    first = second = 0

    # Split elements into two groups using set_bit.
    for x in arr:
        if x & set_bit:
            first ^= x
        else:
            second ^= x

    return sorted([first, second])


if __name__ == "__main__":
    print(findUniquePair([2, 2, 5, 5, 6, 7]))
    print(findUniquePair([1, 3, 4, 1]))
C#
using System;
using System.Collections.Generic;

class GFG {
    static List<int> findUniquePair(int[] arr) {
        int xr = 0;

        // XOR of all elements gives XOR of the two unique numbers.
        foreach (int x in arr) {
            xr ^= x;
        }

        int setBit = xr & -xr;
        int first = 0, second = 0;

        // Split elements into two groups using setBit.
        foreach (int x in arr) {
            if ((x & setBit) != 0) {
                first ^= x;
            } else {
                second ^= x;
            }
        }

        if (first > second) {
            int temp = first;
            first = second;
            second = temp;
        }

        return new List<int> {first, second};
    }

    static void Main() {
        Console.WriteLine(string.Join(" ", findUniquePair(new int[] {2, 2, 5, 5, 6, 7})));
        Console.WriteLine(string.Join(" ", findUniquePair(new int[] {1, 3, 4, 1})));
    }
}
JavaScript
function findUniquePair(arr) {
    let xr = 0;

    // XOR of all elements gives XOR of the two unique numbers.
    for (const x of arr) {
        xr ^= x;
    }

    const setBit = xr & -xr;
    let first = 0, second = 0;

    // Split elements into two groups using setBit.
    for (const x of arr) {
        if (x & setBit) {
            first ^= x;
        } else {
            second ^= x;
        }
    }

    if (first > second) {
        [first, second] = [second, first];
    }

    return [first, second];
}

// Driver Code
console.log(findUniquePair([2, 2, 5, 5, 6, 7]).join(" "));
console.log(findUniquePair([1, 3, 4, 1]).join(" "));

Output
6 7
3 4
Comment