Skip to content

Rust challenge 3 #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions challenge_3/rust/makernaren/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions challenge_3/rust/makernaren/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "makernaren"
version = "0.1.0"
authors = ["naren <[email protected]>"]

[dependencies]
20 changes: 20 additions & 0 deletions challenge_3/rust/makernaren/README.md
Original file line number Diff line number Diff line change
@@ -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
```
7 changes: 7 additions & 0 deletions challenge_3/rust/makernaren/src/main.rs
Original file line number Diff line number Diff line change
@@ -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));

}
32 changes: 32 additions & 0 deletions challenge_3/rust/makernaren/src/multi_occurance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::collections::HashMap;

// Finds the unique character in given array
pub fn find_majority(input: Vec<i32>) -> 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<i32, i32> = 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));
}
}