Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Created April 21, 2015 13:49
Show Gist options
  • Select an option

  • Save steveklabnik/09390c8f76f74b6e2c80 to your computer and use it in GitHub Desktop.

Select an option

Save steveklabnik/09390c8f76f74b6e2c80 to your computer and use it in GitHub Desktop.
fn remove_first<T:Eq + Clone>(i: T, v: Vec<T>) -> Vec<T> {
let index = v.iter().position(|x| *x == i);
let mut result = vec![];
if index != None {
if index.unwrap() > 0 {
result.extend(v[0 .. index.unwrap()].to_vec());
}
result.extend(v[index.unwrap() + 1 .. v.len()].to_vec());
} else {
result.extend(v);
}
return result;
}
fn overlap<T: Eq + Clone>(a: Vec<T>, b: Vec<T>) -> Vec<T> {
let mut result = a;
for n in b.iter() {
result = remove_first(n.clone(), result);
}
result.extend(b);
return result;
}
fn main() {
let mut a = vec![1, 1, 2, 3, 5, 5, 5];
let mut b = vec![1, 1, 2, 2, 3, 4, 5, 5];
let mut o = vec![];
let expected = [1, 1, 2, 2, 3, 4, 5, 5, 5];
o = overlap(a.clone(), b.clone());
o.sort();
assert_eq!(o, expected);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment