Created
July 7, 2018 16:18
-
-
Save mykhailokrainik/d277f1db307fe52a200b86f0095d34f8 to your computer and use it in GitHub Desktop.
Delete duplicate in an Array
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 uniq(arr: &[i32]) -> Vec<i32> { | |
let mut res = vec![arr[0]]; | |
for a in arr.iter().skip(1) { | |
let mut uniq = true; | |
for n in res.iter() { | |
if a == n { | |
uniq = false; | |
} | |
} | |
if uniq { res.push(*a) }; | |
}; | |
res | |
} | |
#[test] | |
fn test_uniq() { | |
assert_eq!( | |
uniq(&[2, 45, 7, 45, 11, -7, 8, 7, 992]), | |
vec![2, 45, 7, 11, -7, 8, 992] | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment