From 267f78bcd760a2adda7518d8b3ed2227ca7ed20d Mon Sep 17 00:00:00 2001 From: naren Date: Fri, 20 Jan 2017 08:25:16 +0530 Subject: [PATCH 1/4] Add multi_occurance module with test cases --- .../rust/makernaren/src/multi_occurance.rs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 challenge_3/rust/makernaren/src/multi_occurance.rs 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 From a1b60e320f6d5a502df65be2ff1cd3ba2d90fccc Mon Sep 17 00:00:00 2001 From: naren Date: Fri, 20 Jan 2017 08:25:41 +0530 Subject: [PATCH 2/4] Add main module to call and use multi_occurances --- challenge_3/rust/makernaren/src/main.rs | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 challenge_3/rust/makernaren/src/main.rs 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 From 023f7e3b0b70018454f6e9c0559b375e99f53c9e Mon Sep 17 00:00:00 2001 From: naren Date: Fri, 20 Jan 2017 08:25:56 +0530 Subject: [PATCH 3/4] Add README.md --- challenge_3/rust/makernaren/README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 challenge_3/rust/makernaren/README.md 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 +``` From 9195cfb26aace010041372b48ff8fb140369c93a Mon Sep 17 00:00:00 2001 From: naren Date: Fri, 20 Jan 2017 08:26:22 +0530 Subject: [PATCH 4/4] Cargo generated files --- challenge_3/rust/makernaren/Cargo.lock | 4 ++++ challenge_3/rust/makernaren/Cargo.toml | 6 ++++++ 2 files changed, 10 insertions(+) create mode 100644 challenge_3/rust/makernaren/Cargo.lock create mode 100644 challenge_3/rust/makernaren/Cargo.toml 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]