Last Occurrence [Linear Search] easy
Find and print the index of the last occurrence of element in the array.

Daniel Gustaw
• 2 min read
![Last Occurrence [Linear Search] easy](/_astro/f0cf18ae-5174-47c0-81af-cb479a0c36b3_ZgPIbu.png)
You have been given an array of size N consisting of integers. In addition you have been given an element M you need to find and print the index of the last occurrence of this element M in the array if it exists in it, otherwise print -1. Consider this array to be 1 indexed.
Input Format:
The first line consists of 2 integers N and M denoting the size of the array and the element to be searched for in the array respectively . The next line contains N space separated integers denoting the elements of of the array.
Output Format
Print a single integer denoting the index of the last occurrence of integer M in the array if it exists, otherwise print -1.
Constraints
SAMPLE INPUT
5 1
1 2 3 4 1
SAMPLE OUTPUT
5
Solution
In main.rs
we can add main function that process in and out streams.
use linear_sort_reverse_search_rust_easy::reverse_search;
use std::io;
fn main() -> io::Result<()> {
reverse_search(&mut io::stdin(), &mut io::stdout())
}
in lib.rs
there is rest of our code
use std::io::{Error, Read, Write};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basic_test() {
let mut output: Vec<u8> = Vec::new();
reverse_search(&mut "5 1
1 2 3 4 1".as_bytes(), &mut output).unwrap();
assert_eq!(&output, b"5\n");
}
#[test]
fn not_found_test() {
let mut output: Vec<u8> = Vec::new();
reverse_search(&mut "5 7
1 2 3 4 1".as_bytes(), &mut output).unwrap();
assert_eq!(&output, b"-1\n");
}
}
pub fn reverse_search(
handle: &mut impl Read ,
output: &mut impl Write,
) -> Result<(), Error> {
let mut buffer = "".to_string();
let mut out = "".to_string();
handle.read_to_string(&mut buffer)?;
let mut lines = buffer.lines();
let mut some_line = match lines.next() {
Some(line) => line,
_ => ""
};
let mut iterator = some_line.split_ascii_whitespace();
let mut len: usize = match iterator.next() {
Some(p) => p.trim().parse().expect("can't read"),
None => 0
};
let needle = match iterator.next() {
Some(p) => p.trim().parse().expect("can't read"),
None => 0
};
some_line = match lines.next() {
Some(line) => line,
_ => ""
};
let mut vec:Vec<i32> = vec![0; len];
iterator = some_line.split_ascii_whitespace();
for n in 0..len {
vec[n] = match iterator.next() {
Some(p) => p.trim().parse().expect("can't read"),
None => 0
};
}
let mut iter = vec.iter().rev();
while let Some(num) = iter.next() {
if *num == needle {
out = format!("{}\n", len);
break;
}
len -= 1;
}
if len == 0 {
out = format!("-1\n");
}
output.write_all(out.to_uppercase().as_bytes())?;
Ok(())
}
Other articles
You can find interesting also.

Process Control in Node JS
Learn how to create and kill child processes in Node JS, dynamically manage their quantity, and conduct bidirectional communication with them.

Daniel Gustaw
• 16 min read

Calculating the Difference Between JSON Files
Learn how to find missing translations in JSON files with dictionaries.

Daniel Gustaw
• 3 min read

Installation of a renewable TLS certificate (certbot + apache on Ubuntu)
There are many methods to obtain a certificate that allows encrypting HTTP traffic. One of them is installing Certbot and using it in conjunction with the Apache server.

Daniel Gustaw
• 2 min read