First index of elementSend FeedbackTake an array with n elements with possibly duplicate elements as the input. The task is to find the index of the first occurrences of the element x in the array and, if it is not present, return -1.Input Format: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 Format:The only line of the output prints the Index or -1.Constraints:1 <= N <= 10^31 <= arr[i] <= 10^91 <= x < NSample Input 1 :87 5 2 11 2 43 1 12Sample Output 1 :2Explanation of Sample Input 1:2 is present twice in the input array and the first time it appears is at index 2.Sample Input 2 :87 5 2 11 2 43 1 110Sample Output 2 :-1Explanation of Sample Input 2:10 is not present in the array so the output is -1.
Question
First index of elementSend FeedbackTake an array with n elements with possibly duplicate elements as the input. The task is to find the index of the first occurrences of the element x in the array and, if it is not present, return -1.Input Format: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 Format:The only line of the output prints the Index or -1.Constraints:1 <= N <= 10^31 <= arr[i] <= 10^91 <= x < NSample Input 1 :87 5 2 11 2 43 1 12Sample Output 1 :2Explanation of Sample Input 1:2 is present twice in the input array and the first time it appears is at index 2.Sample Input 2 :87 5 2 11 2 43 1 110Sample Output 2 :-1Explanation of Sample Input 2:10 is not present in the array so the output is -1.
Solution
The problem is asking to implement a function that will take an array and an integer as input. The function should return the index of the first occurrence of the integer in the array. If the integer is not present in the array, the function should return -1.
Here is a step-by-step solution in Python:
- Define the function with two parameters: the array and the integer.
def find_first_index(arr, x):
- Use the built-in
index()function of the list to find the first occurrence of the integer in the array. This function will return the index of the first occurrence of the integer. If the integer is not present in the array, it will raise aValueErrorexception.
try:
return arr.index(x)
- If a
ValueErrorexception is raised, catch it and return -1.
except ValueError:
return -1
The complete function is:
def find_first_index(arr, x):
try:
return arr.index(x)
except ValueError:
return -1
You can use this function like this:
print(find_first_index([7, 5, 2, 11, 2, 43, 1, 1], 2)) # Output: 2
print(find_first_index([7, 5, 2, 11, 2, 43, 1, 1], 10)) # Output: -1
Similar Questions
Given an array of size 3X+1, where every element occurs three times, except one element, which occurs only once. Find the element that occurs only once.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array (of the form 3X + 1) and second line contains the elements of the array.Output FormatFor each test case, print the number which occurs only once, separated by new line.Constraints30 points1 <= T <= 1001 <= N <= 10370 points1 <= T <= 1001 <= N <= 106ExampleInput2105 7 8 7 7 9 5 9 5 9710 20 20 30 20 30 30Output810
Given an unsorted array Arr[] of N integers and a Key which is present in this array. You need to write a program to find the start index( index where the element is first found from left in the array ) and end index( index where the element is first found from right in the array ).If the key does not exist in the array then return -1 for both start and end index in this case.
Given an array Arr of N positive integers. Your task is to find the elements whose value is equal to that of its index value ( Consider 1-based indexing ).
aran wants to develop a program that uses linear search to find the majority element in an array, which is an element appearing more than n-2 times, where n is the size of the array. The program should input the size of the array and its elements. Display the majority element if found, and indicate if no majority element is present in the array.Help Saran in developing the program.Input format :The first line of input consists of an integer n, representing the number of elements in the array.The second line consists of n space-separated integers, representing the array elements.Output format :The output prints an integer representing the majority element.If no such element is found, print "No majority element found."
Alex wants to create a program to search a target value in a sorted array. The program should input the array size and elements, as well as the target value. Utilizing binary search, it should determine if the target is present, and if so, provide the index. If not, display a message indicating its absence.Assist Alex in completing the program efficiently.Input format :The first line of input consists of an integer N, representing the size of the sorted array.The second line consists of N space-separated integers, the elements of the sorted array in ascending order.The third line consists of an integer target, the value to search for in the array.Output format :If the target is present in the array, print "The target value X is present at index Y", where X is the target element and Y is the index position (index starts from 0).If the target is not present, print "The target value X is not present in the array", where X is the target element.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.