loading...

re: Daily Coding Problem #2 VIEW POST

FULL DISCUSSION
 

Simple Rust solution:

fn product_array(array: &[i32]) -> Vec<i32> {
    let product = array.iter().fold(1, |acc, n| acc * n);
    array.iter().map(|&n| product / n).collect()
}

For follow-up question:

fn product_array2(array: &[i32]) -> Vec<i32> {
    array
        .iter()
        .map(|&n| {
            array
                .iter()
                .filter(|&i| *i != n)
                .fold(1, |acc, val| acc * val)
        }).collect()
}
Code of Conduct Report abuse