githubEdit

22. Unique Number I

The problem can be found at the following link: Question Linkarrow-up-right

Problem Description

You are given an unsorted array of positive integers in which every element occurs exactly twice, except for one element that appears only once. Your task is to find that unique number.

Examples

Example 1:

Input:

arr[] = [1, 2, 1, 5, 5]

Output:

2

Explanation:

All elements except 2 occur twice. Hence, the unique number is 2.

Example 2:

Input:

Output:

Explanation:

Only 20 appears once. All other elements occur twice.

Constraints:

  • $(1 \leq \text{arr.size()} \leq 10^6)$

  • $(0 \leq \text{arr}[i] \leq 10^9)$

My Approach

Bit Manipulation (XOR Method)

  • The XOR of two equal numbers is 0.

  • XOR of a number with 0 is the number itself.

  • Thus, XOR-ing all numbers will cancel out the duplicates and leave the unique one.

Algorithm Steps:

  1. Initialize result as 0.

  2. Iterate over the array and XOR each number with the result.

  3. Final result is the unique number.

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), as we traverse the array once.

  • Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of extra space.

Code (C++)

chevron-rightโšก Alternative Approacheshashtag

๐Ÿ“Š 2๏ธโƒฃ Hash Map Frequency Count

Algorithm Steps:

  1. Traverse the array and store frequencies in a hash map.

  2. Return the element with frequency 1.

๐Ÿ“ Complexity Analysis:

  • Time Complexity: O(n)

  • Space Complexity: O(n)

โœ… Why This Approach?

Straightforward and useful when duplicates can appear more than twice or in non-pair counts.

๐Ÿ“Š 3๏ธโƒฃ Sorting and Pair Check

Algorithm Steps:

  1. Sort the array.

  2. Compare elements in pairs. The one not matching its pair is unique.

๐Ÿ“ Complexity Analysis:

  • Time Complexity: O(n log n)

  • Space Complexity: O(1)

โœ… Why This Approach?

Good when extra space isnโ€™t allowed but we can afford sorting time.

๐Ÿ†š Comparison of Approaches

Approach

โฑ๏ธ Time Complexity

๐Ÿ—‚๏ธ Space Complexity

โœ… Pros

โš ๏ธ Cons

XOR Method

๐ŸŸข O(n)

๐ŸŸข O(1)

Fastest, minimal space

Only works with exact pairs

Hash Map Frequency

๐ŸŸข O(n)

๐Ÿ”ด O(n)

Easy to extend to generic cases

Extra memory used

Sorting + Pairing

๐Ÿ”ด O(n log n)

๐ŸŸข O(1)

No extra memory, simple comparison

Slower due to sorting

โœ… Best Choice?

Scenario

Recommended Approach

โœ… Optimal runtime and no extra space

๐Ÿฅ‡ XOR Method

โœ… Handles non-standard frequencies

๐Ÿฅˆ Hash Map Frequency

โœ… Array can be modified and space is a concern

Sorting + Pair Check

๐Ÿ”น Overall Best for performance and space: XOR Method ๐Ÿ”น Best for generic input flexibility: Hash Map Approach

Code (Java)

Code (Python)

Contribution and Support:

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questionsarrow-up-right. Letโ€™s make this learning journey more collaborative!

โญ If you find this helpful, please give this repository a star! โญ


๐Ÿ“Visitor Count

Last updated