Search
 
SCRIPT & CODE EXAMPLE
 

RUST

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 ));
}
Comment

PREVIOUS NEXT
Code Example
Rust :: primitive data types in rust 
Rust :: rust run tests without cargo 
Rust :: rust tuple vs vec 
Rust :: rust hashset 
Lua :: roblox check if player has gamepass 
Lua :: roblox make a rainbow part 
Lua :: How to make an NPC chat in roblox 
Lua :: how to delete a key in a table lua 
Lua :: lua calculate average number 
Lua :: roblox __index and __newindex 
Lua :: luau loop all players 
Lua :: forever loop in lua 
Lua :: roblox studio part color randomiser 
Lua :: roblox wait for character 
Lua :: return lua 
Lua :: for loop roblox 
Lua :: lua len array 
Lua :: how to make a welcome badge roblox lua 
Lua :: lua string to binary 
Lua :: lua substring | get char from from index 
Lua :: lua math.random 
Lua :: lua add to table 
Matlab :: matlab measure time 
Matlab :: matlab function files 
Matlab :: SAVE TABLE IN MATLAB 
Basic :: split to arraylist vb.net 
Elixir :: elixir hello world 
Elixir :: .t() elixir 
Scala :: How to declare constant variable in scala 
Actionscript :: rabbitmq docker cant connect localhost 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =