Skip to content

Conversation

@AliAlimohammadi
Copy link
Contributor

Description

Adds a new function find_unique_number to the bit_manipulation module that finds the unique element in an array where all other elements appear exactly twice.

Algorithm

Uses the XOR bitwise operation with the property that a ^ a = 0 and a ^ 0 = a. By XORing all elements together, paired numbers cancel out, leaving only the unique number.

Implementation Details

  • Time Complexity: O(n) - single pass through the array
  • Space Complexity: O(1) - constant extra space
  • Uses functional approach with fold() iterator
  • Includes error handling for empty input
  • Comprehensive unit tests covering edge cases

Testing

  • ✅ All existing tests pass
  • ✅ New tests added for:
    • Basic functionality
    • Single element arrays
    • Empty arrays (error case)
    • Negative numbers
    • Large numbers
    • Zero as unique element

Checklist

  • Code follows Rust style guidelines (cargo fmt)
  • Passes clippy lints (cargo clippy)
  • All tests pass (cargo test)
  • Documentation includes examples
  • Function is properly exported in module

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.71%. Comparing base (7a261d7) to head (511ab9a).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #973      +/-   ##
==========================================
- Coverage   95.71%   95.71%   -0.01%     
==========================================
  Files         346      347       +1     
  Lines       22608    22642      +34     
==========================================
+ Hits        21639    21671      +32     
- Misses        969      971       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AliAlimohammadi
Copy link
Contributor Author

@siriak, this is ready to be merged.

@siriak siriak merged commit ed6f1a1 into TheAlgorithms:master Dec 18, 2025
7 checks passed
@AliAlimohammadi AliAlimohammadi deleted the add-find-unique-number branch December 18, 2025 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants