diff --git a/DIRECTORY.md b/DIRECTORY.md index 1c08021c544..dcf778b1a53 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -28,6 +28,7 @@ * [Swap Odd and Even Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/swap_odd_even_bits.rs) * [Trailing Zeros](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/binary_count_trailing_zeros.rs) * [Two's Complement](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/twos_complement.rs) + * [Unique Number](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/find_unique_number.rs) * Ciphers * [AES](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/aes.rs) * [Another ROT13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/another_rot13.rs) diff --git a/src/bit_manipulation/find_unique_number.rs b/src/bit_manipulation/find_unique_number.rs new file mode 100644 index 00000000000..81c9ddfb32c --- /dev/null +++ b/src/bit_manipulation/find_unique_number.rs @@ -0,0 +1,85 @@ +/// Finds the unique number in a slice where every other element appears twice. +/// +/// This function uses the XOR bitwise operation. Since XOR has the property that +/// `a ^ a = 0` and `a ^ 0 = a`, all paired numbers cancel out, leaving only the +/// unique number. +/// +/// # Arguments +/// +/// * `arr` - A slice of integers where all elements except one appear exactly twice +/// +/// # Returns +/// +/// * `Ok(i32)` - The unique number that appears only once +/// * `Err(String)` - An error message if the input is empty +/// +/// # Examples +/// +/// ``` +/// # use the_algorithms_rust::bit_manipulation::find_unique_number; +/// assert_eq!(find_unique_number(&[1, 1, 2, 2, 3]).unwrap(), 3); +/// assert_eq!(find_unique_number(&[4, 5, 4, 6, 6]).unwrap(), 5); +/// assert_eq!(find_unique_number(&[7]).unwrap(), 7); +/// assert_eq!(find_unique_number(&[10, 20, 10]).unwrap(), 20); +/// assert!(find_unique_number(&[]).is_err()); +/// ``` +pub fn find_unique_number(arr: &[i32]) -> Result { + if arr.is_empty() { + return Err("input list must not be empty".to_string()); + } + + let result = arr.iter().fold(0, |acc, &num| acc ^ num); + Ok(result) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_basic_case() { + assert_eq!(find_unique_number(&[1, 1, 2, 2, 3]).unwrap(), 3); + } + + #[test] + fn test_different_order() { + assert_eq!(find_unique_number(&[4, 5, 4, 6, 6]).unwrap(), 5); + } + + #[test] + fn test_single_element() { + assert_eq!(find_unique_number(&[7]).unwrap(), 7); + } + + #[test] + fn test_three_elements() { + assert_eq!(find_unique_number(&[10, 20, 10]).unwrap(), 20); + } + + #[test] + fn test_empty_array() { + assert!(find_unique_number(&[]).is_err()); + assert_eq!( + find_unique_number(&[]).unwrap_err(), + "input list must not be empty" + ); + } + + #[test] + fn test_negative_numbers() { + assert_eq!(find_unique_number(&[-1, -1, -2, -2, -3]).unwrap(), -3); + } + + #[test] + fn test_large_numbers() { + assert_eq!( + find_unique_number(&[1000, 2000, 1000, 3000, 3000]).unwrap(), + 2000 + ); + } + + #[test] + fn test_zero() { + assert_eq!(find_unique_number(&[0, 1, 1]).unwrap(), 0); + } +} diff --git a/src/bit_manipulation/mod.rs b/src/bit_manipulation/mod.rs index 606dc81e89e..ed7a5de3245 100644 --- a/src/bit_manipulation/mod.rs +++ b/src/bit_manipulation/mod.rs @@ -2,6 +2,7 @@ mod binary_coded_decimal; mod binary_count_trailing_zeros; mod counting_bits; mod find_previous_power_of_two; +mod find_unique_number; mod highest_set_bit; mod is_power_of_two; mod n_bits_gray_code; @@ -14,6 +15,7 @@ pub use self::binary_coded_decimal::binary_coded_decimal; pub use self::binary_count_trailing_zeros::binary_count_trailing_zeros; pub use self::counting_bits::count_set_bits; pub use self::find_previous_power_of_two::find_previous_power_of_two; +pub use self::find_unique_number::find_unique_number; pub use self::highest_set_bit::find_highest_set_bit; pub use self::is_power_of_two::is_power_of_two; pub use self::n_bits_gray_code::generate_gray_code;