Find the Index of an Element
Problem Statement
Given an array of n elements and a target element, find the index (position) of the target element in the array.
- The array uses 0-based indexing
- If the element exists multiple times, return the first occurrence
- If the element is not present, return -1
Example 1
Input
arr = [5, 10, 15, 20, 25]
target = 15
Output
2
Explanation
- Element 15 is found at index 2
Example 2
Input
arr = [1, 3, 5, 7]
target = 4
Output
-1
Explanation
- Element 4 does not exist in the array
Why This Problem Is Important
- Tests basic array traversal
- Commonly asked as a warm-up interview question
- Foundation for:
- Linear search
- Binary search
- Index-based array operations
Approaches to Solve the Problem
- Linear Search (Works for all arrays)
- Using Built-in Functions (Language Optimized)
Approach 1: Linear Search
Idea
- Traverse the array from index 0
- Compare each element with the target
- If found, return its index
- If traversal ends, return -1
Algorithm
- For i = 0 to n-1
- If arr[i] == target, return i
- Return -1 if not found
Time and Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(1)
C Implementation
#include
int main() {
int arr[] = {5, 10, 15, 20, 25};
int n = 5;
int target = 15;
int index = -1;
for(int i = 0; i < n; i++) {
if(arr[i] == target) {
index = i;
break;
}
}
printf("Index: %d", index);
return 0;
}
C++ Implementation
#include
using namespace std;
int main() {
int arr[] = {5, 10, 15, 20, 25};
int n = 5;
int target = 15;
int index = -1;
for(int i = 0; i < n; i++) {
if(arr[i] == target) {
index = i;
break;
}
}
cout << "Index: " << index;
}
Java Implementation
public class FindIndex {
public static void main(String[] args) {
int[] arr = {5, 10, 15, 20, 25};
int target = 15;
int index = -1;
for(int i = 0; i < arr.length; i++) {
if(arr[i] == target) {
index = i;
break;
}
}
System.out.println("Index: " + index);
}
}
Python Implementation
arr = [5, 10, 15, 20, 25]
target = 15
index = -1
for i in range(len(arr)):
if arr[i] == target:
index = i
break
print("Index:", index)
C# Implementation
using System;
class Program {
static void Main() {
int[] arr = {5, 10, 15, 20, 25};
int target = 15;
int index = -1;
for(int i = 0; i < arr.Length; i++) {
if(arr[i] == target) {
index = i;
break;
}
}
Console.WriteLine("Index: " + index);
}
}
JavaScript Implementation
let arr = [5, 10, 15, 20, 25];
let target = 15;
let index = -1;
for(let i = 0; i < arr.length; i++) {
if(arr[i] === target) {
index = i;
break;
}
}
console.log("Index:", index);
Approach 2: Using Built-in Methods
Idea
- Many languages provide optimized built-in methods to find the index of an element.
- These methods internally perform a linear search.
- If the element is found → return its index.
- If not found → return -1 (or equivalent).
Time & Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(1)
C Implementation (Using Function)
C has no direct built-in, but we can simulate a reusable function.
#include
int findIndex(int arr[], int n, int target) {
for(int i = 0; i < n; i++) {
if(arr[i] == target)
return i;
}
return -1;
}
int main() {
int arr[] = {5, 10, 15, 20, 25};
int target = 15;
printf("Index: %d", findIndex(arr, 5, target));
return 0;
}
C++ Implementation (STL find)
#include
#include
using namespace std;
int main() {
int arr[] = {5, 10, 15, 20, 25};
int n = 5;
int target = 15;
auto it = find(arr, arr + n, target);
if(it != arr + n)
cout << "Index: " << (it - arr);
else
cout << "Index: -1";
}
Java Implementation (Arrays.asList)
import java.util.*;
public class FindIndexBuiltIn {
public static void main(String[] args) {
Integer[] arr = {5, 10, 15, 20, 25};
int target = 15;
int index = Arrays.asList(arr).indexOf(target);
System.out.println("Index: " + index);
}
}
📌 indexOf() returns -1 if not found.
Python Implementation (index)
arr = [5, 10, 15, 20, 25]
target = 15
index = arr.index(target) if target in arr else -1
print("Index:", index)
C# Implementation (Array.IndexOf)
using System;
class Program {
static void Main() {
int[] arr = {5, 10, 15, 20, 25};
int target = 15;
int index = Array.IndexOf(arr, target);
Console.WriteLine("Index: " + index);
}
}
JavaScript Implementation (indexOf)
let arr = [5, 10, 15, 20, 25];
let target = 15;
console.log("Index:", arr.indexOf(target));
📌 Returns -1 if element does not exist.
Dry Run (Built-in Search)
| Step | Operation | Result |
|---|---|---|
| 1 | Search starts from index 0 | |
| 2 | Compare 5 → not match | |
| 3 | Compare 10 → not match | |
| 4 | Compare 15 → match | |
| ✅ | Return index | 2 |
Summary
| Approach | Time | Space | Notes |
|---|---|---|---|
| Linear Search (Manual) | O(n) | O(1) | Preferred in interviews |
| Built-in Methods | O(n) | O(1) | Cleaner, practical usage |
Key Takeaways
- Always returns first occurrence
- Works for sorted & unsorted arrays
- -1 cleanly handles “not found”
- Built-ins = convenience, not magic
