Problem for reference: https://i.imgur.com/KfV8Kcl.png

My working solution in D is this:

`int firstDuplicate(int[] a) { foreach (n; a) { if (a[abs(n) - 1] < 0) return abs(n); a[abs(n) - 1] = -a[abs(n) - 1]; } return -1; }`

And my rust attempt looks like:

`fn firstDuplicate(mut a: Vec<i32>) -> i32 { for n in a { if a[n.abs() - 1] < 0 { return n.abs(); } a[n.abs() - 1] = -a[n.abs() - 1]; } -1 }`

But I am not sure how to get around the usize i32 conversion, or the issues with mut and immutable borrows. I have a hashmap solution in Rust that works but I’m trying to do it as

above too.

Thanks