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 <= 10^{6}
0 <= arr[i] <= 10^{6}
0 <= x <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Complete search

Starter code: #User function Template for python3



class Solution:

    #Complete the below function

    def search(self,arr, N, X):

        #Your code here

Solutions:
class Solution:

	def search(self, arr, N, X):
		for i in range(len(arr)):
			if arr[i] == X:
				return i
		else:
			return -1


class Solution:

	def search(self, arr, N, X):
		for x in range(0, len(arr)):
			if arr[x] == X:
				return x
		return -1


class Solution:

	def search(self, arr, N, X):
		try:
			return arr.index(X)
		except:
			return -1


class Solution:

	def search(self, arr, N, X):
		result = -1
		try:
			result = arr.index(X)
		except ValueError:
			pass
		return result


class Solution:

	def search(self, arr, N, X):
		flag = 0
		for i in range(0, N):
			if arr[i] == X:
				return arr.index(arr[i])
				flag = 1
		if flag == 0:
			return -1


class Solution:

	def search(self, arr, N, X):
		l = 0
		r = N - 1
		for i in range(N):
			if arr[i] == X:
				return i
		return -1


class Solution:

	def search(self, arr, N, X):
		index = 0
		if X in arr:
			for nb in arr:
				if nb == X:
					return index
				index += 1
		else:
			return -1


class Solution:

	def search(self, arr, N, X):
		for number in range(N):
			if arr[number] == X:
				return number
		return -1


class Solution:

	def search(self, arr, N, X):
		for (idx, ele) in enumerate(arr):
			if ele == X:
				return idx
		else:
			return -1


class Solution:

	def search(self, arr, N, X):
		if X not in arr:
			return -1
		else:
			return arr.index(X)


class Solution:

	def search(self, arr, N, X):
		for i in range(N):
			if arr[i] == X:
				return i
		return -1
so = Solution()
so.search([1, 2, 3, 7], 4, 3)


class Solution:

	def search(self, arr, N, X):
		low = 0
		high = N - 1
		while low <= high:
			if arr[low] == X:
				return low
			else:
				low = low + 1
		return -1


class Solution:

	def search(self, arr, N, X):
		for (index, num) in enumerate(arr):
			if num == X:
				return index
		return -1


class Solution:

	def search(self, arr, N, X):
		count = 0
		for i in arr:
			if i == X:
				return count
			else:
				count += 1
		return -1


class Solution:

	def search(self, arr, N, X):
		for i in arr:
			if i == X:
				return arr.index(i)
		else:
			return -1


class Solution:

	def search(self, arr, N, X):
		for i in range(0, N):
			if arr[i] == X:
				return i
				break
		return -1


class Solution:

	def search(self, arr, N, X):
		if X not in arr:
			return -1
		for i in range(N):
			if X == arr[i]:
				return i


class Solution:

	def search(self, arr, N, X):
		if arr.count(X) >= 1:
			return arr.index(X)
		else:
			return -1


class Solution:

	def search(self, arr, N, X):
		for (i, e) in enumerate(arr):
			if e == X:
				return i
		return -1


class Solution:

	def search(self, arr, N, X):
		if X in arr:
			return arr.index(X)
		return -1

	def firstOccurance(self, arr, low, high, x):
		if low <= high:
			mid = (low + high) // 2
			if x < arr[mid]:
				return self.firstOccurance(arr, low, mid - 1, x)
			elif x > arr[mid]:
				return self.firstOccurance(arr, mid + 1, high, x)
			elif mid == 0 or arr[mid - 1] != arr[mid]:
				return mid
			else:
				return self.firstOccurance(arr, low, mid - 1, x)
		return -1


class Solution:

	def search(self, arr, N, X):
		for i in range(N):
			if arr[i] == X:
				ans = i
				break
			else:
				ans = -1
		return ans


class Solution:

	def search(self, A, N, x):
		if x in A:
			return A.index(x)
		return -1


class Solution:

	def search(self, arr, N, X):
		try:
			return arr.index(int(X))
		except ValueError as e:
			return -1


class Solution:

	def search(self, arr, n, x):
		f = 0
		for num in arr[0:n]:
			if x == num:
				f += 1
				break
		if f == 1:
			return arr.index(x)
		else:
			return -1


class Solution:

	def search(self, arr, n, x):
		for i in range(len(arr)):
			if arr[i] == x:
				return i
		return -1