Answers for "Find unique element in array where all other elements occur 3 times, uses boolean logic"

0

Find unique element in array where all other elements occur 3 times, uses boolean logic

// Find the unique element in an array where all other elements occur three times
// Uses boolean logic
fn lone_number(a:&mut [i32] ) -> i32 {
    let mut ones = 0;
    let mut twos = 0;
    for item in a.iter(){
        ones = (ones ^ item) & !twos;
        twos = (twos ^ item) & !ones;
    } 
    ones
}

fn main() {
    let mut a: [i32; 10] = [ 1, 2, 3, 1, 2, 3, 5, 1, 2, 3 ];
    println!("Lone number is {}", lone_number(&mut a ));
}
Posted by: Guest on April-12-2022

Code answers related to "Find unique element in array where all other elements occur 3 times, uses boolean logic"

Browse Popular Code Answers by Language