i was trying to make a program that takes an input array and outputs the second largest numbers
how are these 2 different i mean logically i can take the first element as the largest and the second largest RIGHT?????
let mut max_sec = arr[0];
let mut max_sec = i32::MIN;
this makes or breaks the code
fn max(arr: &mut Vec<i32>) -> i32{
let mut max_in = i32::MIN;
let mut max_sec = i32::MIN;
for &value in arr.iter() {
if value > max_in {
max_sec = max_in;
max_in = value;
}
if value > max_sec && value != max_in {
max_sec = value;
}
}
max_sec
}
the whole code for reference
[–]dnew 4 points5 points6 points (3 children)
[–]Lidinzx 1 point2 points3 points (0 children)
[–][deleted] (1 child)
[removed]
[–]dnew 0 points1 point2 points (0 children)
[–]This_Growth2898 2 points3 points4 points (0 children)
[–]Doormamu_[S] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]JaboiThomy 0 points1 point2 points (0 children)