diff --git a/challenge_3/rust/makernaren/Cargo.lock b/challenge_3/rust/makernaren/Cargo.lock new file mode 100644 index 000000000..e2dd7ff3a --- /dev/null +++ b/challenge_3/rust/makernaren/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "makernaren" +version = "0.1.0" + diff --git a/challenge_3/rust/makernaren/Cargo.toml b/challenge_3/rust/makernaren/Cargo.toml new file mode 100644 index 000000000..55e3673ab --- /dev/null +++ b/challenge_3/rust/makernaren/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "makernaren" +version = "0.1.0" +authors = ["naren "] + +[dependencies] diff --git a/challenge_3/rust/makernaren/README.md b/challenge_3/rust/makernaren/README.md new file mode 100644 index 000000000..e1665c7a7 --- /dev/null +++ b/challenge_3/rust/makernaren/README.md @@ -0,0 +1,20 @@ +## Majority element +#### To run the code : +* Open this directory in terminal and run `cargo run` to compile and run the code. +* To test the code just run, `cargo test` in same directory. +``` +$ cargo run +Compiling makernaren v0.1.0 (file:///path/to/2017Challenges/challenge_3/rust/makernaren) +c Finished debug [unoptimized + debuginfo] target(s) in 0.56 secs + Running `/path/to/2017Challenges/challenge_3/rust/makernaren/target/debug/makernaren` +Given array is [1, 1, 2, 2, 3, 3, 3, 3, 5] + Majority occurace : 3 + +$ cargo test + Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs + Running /path/to/2017Challenges/challenge_3/rust/makernaren/target/debug/makernaren-be343b5c46ce1a8b +running 1 test +test multi_occurance::tests::test_int ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured +``` diff --git a/challenge_3/rust/makernaren/src/main.rs b/challenge_3/rust/makernaren/src/main.rs new file mode 100644 index 000000000..72e066559 --- /dev/null +++ b/challenge_3/rust/makernaren/src/main.rs @@ -0,0 +1,7 @@ +mod multi_occurance; +fn main() { + // let input = vec!['a', 'a', 'b', 'b', 'c', 'c', 'd']; + let input = vec![1, 1, 2, 2, 3, 3, 3, 3, 5]; + println!("Given array is {:?} \n Majority occurace : {}",input.clone(), multi_occurance::find_majority(input)); + +} \ No newline at end of file diff --git a/challenge_3/rust/makernaren/src/multi_occurance.rs b/challenge_3/rust/makernaren/src/multi_occurance.rs new file mode 100644 index 000000000..775867cb3 --- /dev/null +++ b/challenge_3/rust/makernaren/src/multi_occurance.rs @@ -0,0 +1,32 @@ +use std::collections::HashMap; + +// Finds the unique character in given array +pub fn find_majority(input: Vec) -> i32 { + // Create hashmap, let the key be the element in input array + // and value be its count. Then return the key with count of 1. + let mut occurances: HashMap = HashMap::new(); + for key in input { + *occurances.entry(key).or_insert(0) += 1; + } + let mut max: i32 = 0; + let mut element: i32 = 0; + for (key, val) in occurances.iter() { + if *val > max { + max = val.to_owned(); + element = key.to_owned(); + } + } + return element +} + + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_int() { + let expected = 2; + let input = vec![2, 2, 2, 2, 2, 2, 3, 4, 2, 3, 5, 4, 6, 4, 6, 9, 10,9 ,8 ,7 ,8 ,10 ,7]; + assert_eq!(expected.to_owned(), find_majority(input)); + } +} \ No newline at end of file