ADVERTISEMENT
ADVERTISEMENT

Rust program to find the second largest element from the array

Rust | Array Example: Write a program to find the second largest element from the array.
Submitted by Nidhi, on October 20, 2021

Problem Solution:

In this program, we will create an integer array with 5 elements then we will find the second largest element from the array.

Program/Source Code:

The source code to find the second largest element from the array is given below. The given program is compiled and executed successfully.

// Rust program to find the second largest 
// element from array

fn main() {
   let arr:[i32;5] = [13,18,23,14,12];
   let mut large1:i32 = 0;
   let mut large2:i32 = 0;
   let mut i:usize = 1;
   
   large1=arr[0];
 
   while i<arr.len()
   {
	if large1 < arr[i] {
		large2 = large1;
		large1 = arr[i];
	} else if large2 < arr[i] {
		large2 = arr[i];
	}
	i = i + 1;
   }
 
   println!("Second largest element is: {}", large2);
}

Output:

Second largest element is: 18

Explanation:

Here, we created an array of the integer with 5 elements. Then we found the second largest element from the array and printed the result.

Rust Arrays Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.