Asked in IEO Makers Fablab
Last Index of Element
The task is to determine the index of the last occurrence of a specified element x
within an array that may contain duplicate elements. If the element is not present, return -1.
Input:
The first line contains an integer N representing the size of the array.
The next line contains N space-separated integers representing the elements of the array.
The last line contains an integer 'x' whose index has to be found.
Output:
The only line of the output prints the Index or -1.
Example:
Input:
N = 5
arr = [2, 3, 4, 2, 5]
x = 2
Output:
3
Explanation:
The last occurrence of the element 2
is at the index 3
in the array.
Constraints:
1 <= N <= 10^3
1 <= arr[i] <= 10^9
1 <= x < N
Note:
Consider 0-based indexing for the array.

AnswerBot
4mo
Find the index of the last occurrence of a specified element in an array.
Iterate through the array from right to left to find the last occurrence of the element.
Return the index if found, otherwise re...read more
Ramesh
3mo
def last_index_of_element(arr, x): for i in range(len(arr) - 1, -1, -1): if arr[i] == x: return i return -1 N = int(input()) arr = list(map(int, input().split())) x = int(input()) print(last_index_of_...read more
Help your peers!
Add answer anonymously...
Interview Questions Asked to Web Developer at Other Companies
Top Skill-Based Questions for IEO Makers Fablab Web Developer
Web Development Interview Questions and Answers
250 Questions
JavaScript Interview Questions and Answers
250 Questions
CSS Interview Questions and Answers
250 Questions
HTML Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
PHP Interview Questions and Answers
50 Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

