Created
November 10, 2016 04:24
-
-
Save glaznaj/691ab9042e82d356cf719fa57cbaa5eb to your computer and use it in GitHub Desktop.
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
| 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 | |
| } |
Author
glaznaj
commented
Nov 10, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment