ADVERTISEMENT
ADVERTISEMENT

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 »



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.