Search an Element in an array | Practice | GeeksforGeeks

Given an integer array and another integer element. The task is to find if the given element is present in array or not.
Example 1:
Input:
n = 4
arr[] = {1,2,3,4}
x = 3
Output: 2
Explanation: There is one test case 
with array as {1, 2, 3 4} and element 
to be searched as 3.  Since 3 is 
present at index 2, output is 2.
Example 2:
Input:
n = 5
arr[] = {1,2,3,4,5}
x = 5
Output: 4
Explanation: For array elements 
{1,2,3,4,5} element to be searched 
is 5 and it is at index 4. So, the 
output is 4.

Your Task:
The task is to complete the function search() which takes the array arr[], its size N and the element X as inputs and returns the index of first occurrence of X in the given array. If the element X does not exist in the array, the function should return -1.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1). 
Constraints:
1 <= n <= 106
0 <= arr[i] <= 106
0 <= x <= 105
0
ifeelrook2 days ago
int search(int arr[], int N, int X)
{
        
    // Your code here
    for(int i=0;i<N;i++)
    {
        if(arr[i]==X) return i;
    }
    return -1;
        
}
0
rohitkumar016 days ago
C++ Solution:- 
 
for(int i=0; i<N; i++){       if(arr[i]==X){           return i;       }   }   return -1;
0
harshscode1 week ago
for(int i=0;i<n;i++)   {       if(a[i]==x)       return i;   }   return -1;
0
tusharkapoor7431 week ago
Why did binary search didn't work in this question?
I wrote the following code, but during submission it showed too large input.
   int low=0;   int high=N;   int mid;   while(low<=high){       mid=(low+high)/2;       if(X==arr[mid]){           return mid;       }       else if(X<arr[mid]){           high=mid-1;       }       else{           low=mid+1;       }   }   return -1;
0
zerefkhan2 weeks ago
C++ solution
Total Time Taken : 0.23/1.26
 
	int search(int arr[], int N, int X)
    {
        for(int i=0;i<N; i++){if(arr[i]==X) return i;}
        return -1;
    }
+1
sailendrachettri2 weeks ago
C++ Solution
int search(int arr[], int N, int X)
    {
        
        for(int i = 0; i < N; i++){
            if(X == arr[i]){
                return i;
            }
        }
        return -1;
        
    }
0
shashwatchaurasia834 weeks ago
int search(int arr[], int N, int X){          for(int i=0;i<N;i++)   {       if(arr[i]==X)       {           return i;       }   }       return -1;}
0
lakshitpant7741 month ago
class Solution{       static int search(int arr[],int N,int X){       for(int i=0;i<N;i++){           if(arr[i]==X)           return i;       }       return -1;     }
}
0
lakshitpant774
This comment was deleted.
0
atif836141 month ago
java solution 
for(int i=0;i<arr.length;i++) {  if(arr[i]==x) {   return i;  } } return -1;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.