Created
June 8, 2014 20:04
-
-
Save wunki/dee82b7fdf588cb30e8d to your computer and use it in GitHub Desktop.
Unique character check in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::os; | |
fn unique_chars(s: &str) -> bool { | |
let v: Vec<char> = s.chars().collect(); | |
let mut y = v.clone(); | |
y.dedup(); | |
v.len() == y.len() | |
} | |
fn main() -> () { | |
let args = os::args(); | |
let sentence = args.get(1); | |
match unique_chars(sentence.as_slice()) { | |
true => println!("String contains only unique characters"), | |
false => println!("String doesn't contain only unique characters") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will work only for sorted input.
According to official documentation dedup() deletes duplicates only consecutive repeated elements.