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

Rust | Array Example: Write a program to find out the first repeated element in the array.
Submitted by Nidhi, on October 22, 2021

Problem Solution:

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

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.

Rust Arrays Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.