# Day 19: Array Coding Questions Rust Part-1

·

## Introduction:

Let us practice some coding exercises for arrays in Rust lang. Like some common questions like a reverse array, find the maximum number so that we will get familiar with the array.

### Question 1: Find the maximum element in an array.

#### Rust Solution:

``````fn find_max(arr: &[i32]) -> Option<i32> {
if arr.is_empty() {
return None;
}
let mut max = arr[0];
for &num in arr {
if num > max {
max = num;
}
}
Some(max)
}

fn main() {
let numbers = [4, 7, 2, 9, 5];
if let Some(max) = find_max(&numbers) {
println!("The maximum element is: {}", max);
} else {
println!("The array is empty.");
}
}
``````

### Question 2: Calculate the sum of elements in an array.

#### Rust Solution:

``````fn calculate_sum(arr: &[i32]) -> i32 {
let mut sum = 0;
for &num in arr {
sum += num;
}
sum
}

fn main() {
let numbers = [4, 7, 2, 9, 5];
let sum = calculate_sum(&numbers);
println!("The sum of elements is: {}", sum);
}
``````

### Question 3: Reverse an array.

#### Rust Solution:

``````fn reverse_array(arr: &mut [i32]) {
let mut left = 0;
let mut right = arr.len() - 1;
while left < right {
arr.swap(left, right);
left += 1;
right -= 1;
}
}

fn main() {
let mut numbers = [4, 7, 2, 9, 5];
reverse_array(&mut numbers);
println!("Reversed array: {:?}", numbers);
}
``````

### Question 4: Check if an array contains a specific element.

#### Rust Solution:

``````fn contains_element(arr: &[i32], target: i32) -> bool {
for &num in arr {
if num == target {
return true;
}
}
false
}

fn main() {
let numbers = [4, 7, 2, 9, 5];
let target = 7;
if contains_element(&numbers, target) {
println!("The array contains {}.", target);
} else {
println!("The array does not contain {}.", target);
}
}
``````

These sample solutions illustrate basic operations on arrays in Rust, including finding the maximum element, calculating the sum, reversing the array, and checking for a specific element. They showcase common Rust syntax and array manipulation techniques.

#### Rust Solution:

``````fn merge_sorted_arrays(arr1: &[i32], arr2: &[i32]) -> Vec<i32> {
let mut merged = Vec::new();
let (mut i, mut j) = (0, 0);

while i < arr1.len() && j < arr2.len() {
if arr1[i] < arr2[j] {
merged.push(arr1[i]);
i += 1;
} else {
merged.push(arr2[j]);
j += 1;
}
}

merged.extend_from_slice(&arr1[i..]);
merged.extend_from_slice(&arr2[j..]);
merged
}

fn main() {
let arr1 = [1, 3, 5, 7, 9];
let arr2 = [2, 4, 6, 8, 10];
let merged = merge_sorted_arrays(&arr1, &arr2);
println!("Merged and sorted array: {:?}", merged);
}
``````

#### Rust Solution:

``````fn rotate_left(arr: &mut [i32], k: usize) {
let n = arr.len();
let k = k % n;
arr.rotate_left(k);
}

fn main() {
let mut numbers = [1, 2, 3, 4, 5];
let k = 3; // Rotate by 3 positions to the left
rotate_left(&mut numbers, k);
println!("Rotated array: {:?}", numbers);
}
``````

#### Rust Solution:

``````fn find_second_largest(arr: &[i32]) -> Option<i32> {
if arr.len() < 2 {
return None;
}
let mut max = arr[0];
let mut second_max = i32::min_value();

for &num in arr.iter().skip(1) {
if num > max {
second_max = max;
max = num;
} else if num > second_max && num < max {
second_max = num;
}
}
Some(second_max)
}

fn main() {
let numbers = [7, 2, 9, 3, 11, 5];
if let Some(second_largest) = find_second_largest(&numbers) {
println!("Second largest element: {}", second_largest);
} else {
println!("Array too small.");
}
}
``````

### Conclusion:

Mastering array operations in Rust is fundamental for handling data efficiently. Understanding how to iterate through arrays, perform basic computations, and manipulate array elements forms the backbone of many algorithms and data processing tasks. Rust's concise syntax and array handling capabilities facilitate the development of robust and performant code.

Happy coding with Rust!

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.techwasti.com/

\==========================**=========================