Skip to content

Instantly share code, notes, and snippets.

@mykhailokrainik
Created July 7, 2018 16:18
Show Gist options
  • Save mykhailokrainik/d277f1db307fe52a200b86f0095d34f8 to your computer and use it in GitHub Desktop.
Save mykhailokrainik/d277f1db307fe52a200b86f0095d34f8 to your computer and use it in GitHub Desktop.
Delete duplicate in an Array
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