This commit is contained in:
ridethepig 2023-02-19 10:34:14 +00:00
parent 120a427bdb
commit 3e7544fd6b

View File

@ -7,15 +7,38 @@ fn main() {
}
fn add_n(v: Vec<i32>, n: i32) -> Vec<i32> {
unimplemented!()
// The function should return a new vector whose elements are the numbers in the original vector v with n added to each number.
let mut newvec = Vec::new();
for i in v {
newvec.push(i + n);
}
newvec
}
fn add_n_inplace(v: &mut Vec<i32>, n: i32) {
unimplemented!()
// does the same thing as add_n, but modifies v directly (in place) and does not return anything.
for i in v {
*i += n;
}
}
fn dedup(v: &mut Vec<i32>) {
unimplemented!()
// that removes duplicate elements from a vector in-place (i.e. modifies v directly).
// If an element is repeated anywhere in the vector, you should keep the element that appears first.
// You may want to use a HashSet for this.
let mut _hash = HashSet::new();
let mut _repeated = Vec::new();
for i in 0..v.len() {
if _hash.contains(&v[i]) {
_repeated.push(i);
} else {
_hash.insert(v[i]);
}
}
_repeated.reverse();
for i in _repeated {
v.remove(i);
}
}
#[cfg(test)]