Q:

Rust program to find out the first repeated element in the array

belongs to collection: Rust Arrays Programs

0

In this program, we will create an array of integers then we will find the 1st repeated element in the array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to find out the first repeated element in the array is given below. The given program is compiled and executed successfully.

// Rust program to find out the first repeated 
// element in the array

fn main() 
{
	let mut arr:[usize;6] = [0,1,2,3,2,3];
	let mut i:usize=0;
	let mut j:usize=0;
	let mut item:usize=0;
	let mut flag:isize=0;

	while i<5
	{
		j=i;
		while j<=5
		{
			if arr[i] == arr[j] {
				item = arr[j];
				flag = 1;
				break;
			}
			j=j+1;
		}
		i=i+1;
	}

	if flag == 1 {
		println!("{0} repeated @ {1} index", item, j)
	} 
	else {
		println!("There is no repeated element")
	}
}

Output:

2 repeated @ 4 index

Explanation:

Here, we created an array of integers with 6 elements, and then we searched the 1st repeated element in the array and printed the index of repeated elements.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Arrays Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to delete the given item from the arr... >>
<< Rust program to swap adjacent elements of the arra...