`{"id":"1afad1a5-fa14-4068-a460-bf24230974e8","name":"Find Element In An Array","description":"1.You are given a number n, representing the size of array a.\r\n2.You are given n distinct numbers, representing elements of array a.\r\n3. You are given another number d.\r\n4. You are required to check if d number exists in the array a and at what index (0 based). If found print the index, otherwise print -1.","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number d","outputFormat":"A number representing index at which d is found in array a and -1 if not found","constraints":"1 &lt;= n &lt;= 10^7\r\n-10^9 &lt;= n1, n2 \r\n.. n elements &lt;= 10^9\r\n-10^9 &lt;= d &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<iostream>\r\nusing namespace std;\r\n\r\n\r\nint main(){\r\n //write your code here\r\n \r\n \r\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main{\r\n\r\npublic static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"def findElement(arr , data):\n # write your code here\n\n\nif __name__ == '__main__':\n num = int(input())\n arr = [0]*num\n \n for i in range(0 , num):\n val = int(input())\n arr[i] = val\n data = int(input())\n solution = findElement(arr , data)\n print(solution)"}},"points":10,"difficulty":"easy","sampleInput":"6\r\n15\r\n30\r\n40\r\n4\r\n11\r\n9\r\n40","sampleOutput":"2","questionVideo":"https://www.youtube.com/embed/awuSQW2F0AM","hints":[],"associated":[{"id":"bf329330-b066-41f8-b20b-475e3d26e8aa","name":"(Find element) Let us assume you are given an array, arr. What is the inbuilt function to find the number of elements present in an array?","slug":"find-element-let-us-assume-you-are-given-an-array-arr-what-is-the-inbuilt-function-to-find-the-number-of-elements-present-in-an-array","type":4},{"id":"e031ff29-8bfc-48dc-bfac-9f76d0fcd767","name":"(Find element) The correct syntax to initialize an array, arr, if n is the size of the array is :","slug":"find-element-the-correct-syntax-to-initialize-an-array-arr-if-n-is-the-size-of-the-array-is","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f10b54f1-0f44-408f-82d5-89c189f4ad57","name":"Function and Arrays","slug":"function-and-arrays","type":0},{"id":"7d9fc084-acf3-4f4f-b89a-6870943538c4","name":"Find Element In An Array","slug":"find-element-in-an-array","type":1}],"next":{"id":"54dd34d1-cc36-4bac-8b20-e9c3a5f6e44e","name":"Find element in an Array","type":3,"slug":"find-element-in-an-array"},"prev":{"id":"1866f952-37f1-4aaf-a3e2-1638fdd7fc9b","name":"Span of an Array","type":3,"slug":"span-of-an-array"}}}`

# Find Element In An Array

1.You are given a number n, representing the size of array a. 2.You are given n distinct numbers, representing elements of array a. 3. You are given another number d. 4. You are required to check if d number exists in the array a and at what index (0 based). If found print the index, otherwise print -1.

`{"id":"1afad1a5-fa14-4068-a460-bf24230974e8","name":"Find Element In An Array","description":"1.You are given a number n, representing the size of array a.\r\n2.You are given n distinct numbers, representing elements of array a.\r\n3. You are given another number d.\r\n4. You are required to check if d number exists in the array a and at what index (0 based). If found print the index, otherwise print -1.","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number d","outputFormat":"A number representing index at which d is found in array a and -1 if not found","constraints":"1 &lt;= n &lt;= 10^7\r\n-10^9 &lt;= n1, n2 \r\n.. n elements &lt;= 10^9\r\n-10^9 &lt;= d &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<iostream>\r\nusing namespace std;\r\n\r\n\r\nint main(){\r\n //write your code here\r\n \r\n \r\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main{\r\n\r\npublic static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"def findElement(arr , data):\n # write your code here\n\n\nif __name__ == '__main__':\n num = int(input())\n arr = [0]*num\n \n for i in range(0 , num):\n val = int(input())\n arr[i] = val\n data = int(input())\n solution = findElement(arr , data)\n print(solution)"}},"points":10,"difficulty":"easy","sampleInput":"6\r\n15\r\n30\r\n40\r\n4\r\n11\r\n9\r\n40","sampleOutput":"2","questionVideo":"https://www.youtube.com/embed/awuSQW2F0AM","hints":[],"associated":[{"id":"bf329330-b066-41f8-b20b-475e3d26e8aa","name":"(Find element) Let us assume you are given an array, arr. What is the inbuilt function to find the number of elements present in an array?","slug":"find-element-let-us-assume-you-are-given-an-array-arr-what-is-the-inbuilt-function-to-find-the-number-of-elements-present-in-an-array","type":4},{"id":"e031ff29-8bfc-48dc-bfac-9f76d0fcd767","name":"(Find element) The correct syntax to initialize an array, arr, if n is the size of the array is :","slug":"find-element-the-correct-syntax-to-initialize-an-array-arr-if-n-is-the-size-of-the-array-is","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f10b54f1-0f44-408f-82d5-89c189f4ad57","name":"Function and Arrays","slug":"function-and-arrays","type":0},{"id":"7d9fc084-acf3-4f4f-b89a-6870943538c4","name":"Find Element In An Array","slug":"find-element-in-an-array","type":1}],"next":{"id":"54dd34d1-cc36-4bac-8b20-e9c3a5f6e44e","name":"Find element in an Array","type":3,"slug":"find-element-in-an-array"},"prev":{"id":"1866f952-37f1-4aaf-a3e2-1638fdd7fc9b","name":"Span of an Array","type":3,"slug":"span-of-an-array"}}}`

Editor

# Find Element In An Array

easy

1.You are given a number n, representing the size of array a. 2.You are given n distinct numbers, representing elements of array a. 3. You are given another number d. 4. You are required to check if d number exists in the array a and at what index (0 based). If found print the index, otherwise print -1.

## Constraints

1 <= n <= 10^7 -10^9 <= n1, n2 .. n elements <= 10^9 -10^9 <= d <= 10^9

## Format

### Input

A number n n1 n2 .. n number of elements A number d

### Output

A number representing index at which d is found in array a and -1 if not found

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}6 15 30 40 4 11 9 40```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}2`

Question Video

Discussions

Show Discussion

Related Resources