Skip to content

Instantly share code, notes, and snippets.

@glaznaj
Created November 10, 2016 04:24
Show Gist options
  • Select an option

  • Save glaznaj/691ab9042e82d356cf719fa57cbaa5eb to your computer and use it in GitHub Desktop.

Select an option

Save glaznaj/691ab9042e82d356cf719fa57cbaa5eb to your computer and use it in GitHub Desktop.
fn main() {
let arr_num = vec![16, 3, 8, 16, 4, 4, 3, 1, 16];
assert_eq!(array_unique(&arr_num), [1, 3, 4, 8, 16]);
let arr_str = vec!["ab", "ba", "cd", "ba", "aa", "dd", "ba", "cd", "aa"];
assert_eq!(array_unique(&arr_str), ["aa", "ab", "ba", "cd", "dd"]);
}
fn array_unique<T: Ord + Clone>(arr: &[T]) -> Vec<T> {
let mut result = arr.to_owned();
result.sort();
let mut k = result.len() - 1;
while k > 0 {
if result[k] == result[k - 1] {
result.remove(k);
}
k -= 1;
}
result
}
@glaznaj
Copy link
Copy Markdown
Author

glaznaj commented Nov 10, 2016

fn array_unique<T: Ord + Clone>(arr: &[T]) -> Vec<T> {
    let mut result = arr.to_owned();
    result.sort();
    result.dedup();
    result
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment