First and Last Occurrences in an Array

Last Updated : 5 Apr, 2026

Given an array of integers, task is to find the starting and ending position of a given key. 

Input: arr[] = [1, 2, 3, 4, 5, 5] , key = 5
Output: [4, 5]
Explanation: 5 appears first time at index 4 and appears last time at index 5(0 based indexing)

Input: arr = [6, 5, 4, 3, 1, 2] , key = 4
Output: [2, 2]
Explanation: 4 appears first time and last time at index 2.

Input: arr = [7, 8, 6] , key = 2
Output: [-1, -1]
Explanation: Since 2 does not appear in the array, we will return -1 for both the start and end indices..

Try It Yourself
redirect icon

We follow two steps to find the first and last occurrences.

  • Traverse from the left and stop as soon as a match is found, store the index as start
  • If no occurrence is found, return {-1, -1}
  • Now traverse from right and stop as soon as a match is found, store the index as end
  • Return {start, end}
C++
#include <bits/stdc++.h>
using namespace std;

vector<int> findIndex(vector<int>& arr, int key) {
    int n = arr.size();        
    int start = -1;          

    // Step 1: Find first occurrence of key
    for (int i = 0; i < n; i++) {
        if (arr[i] == key) {   
            start = i;           
            break;              
        }
    }

    // If key is not found at all
    if (start == -1) {
        return {-1, -1};         
    }

    int end = start;             

    // Step 2: Find last occurrence of key
    for (int i = n - 1; i >= 0; i--) {
        if (arr[i] == key) {   
            end = i;            
            break;              
        }
    }

    return {start, end};        
}

int main() {
    vector<int> arr = {1, 2, 3, 4, 5, 5};
    int key = 5;

    vector<int> result = findIndex(arr, key);

    cout << result[0] << " " << result[1] << endl;

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

// Function to find first and last index of key
int* findIndex(int* arr, int n, int key) {
    static int result[2] = {-1, -1};
    int start = -1;

    // Step 1: Find first occurrence of key
    for (int i = 0; i < n; i++) {
        if (arr[i] == key) {
            start = i;
            break;
        }
    }

    if (start == -1) {
        return result;
    }

    int end = start;

    // Step 2: Find last occurrence of key
    for (int i = n - 1; i >= 0; i--) {
        if (arr[i] == key) {
            end = i;
            break;
        }
    }

    result[0] = start;
    result[1] = end;
    return result;
}

int main() {
    int arr[] = {1, 2, 3, 4, 5, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    int key = 5;
    int *result = findIndex(arr, n, key);
    printf("%d %d\n", result[0], result[1]);
    return 0;
}
Java
import java.util.*;

public class Main {
    public static int[] findIndex(int[] arr, int key) {
        int n = arr.length;
        int start = -1;

        // Step 1: Find first occurrence of key
        for (int i = 0; i < n; i++) {
            if (arr[i] == key) {
                start = i;
                break;
            }
        }

        if (start == -1) {
            return new int[]{-1, -1};
        }

        int end = start;

        // Step 2: Find last occurrence of key
        for (int i = n - 1; i >= 0; i--) {
            if (arr[i] == key) {
                end = i;
                break;
            }
        }

        return new int[]{start, end};
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 5};
        int key = 5;
        int[] result = findIndex(arr, key);
        System.out.println(result[0] + " " + result[1]);
    }
}
Python
def findIndex(arr, key):
    n = len(arr)
    start = -1

    # Step 1: Find first occurrence of key
    for i in range(n):
        if arr[i] == key:
            start = i
            break

    # if key is not found at all
    if start == -1:
        return [-1, -1]

    end = start

    # Step 2: Find last occurrence of key
    for i in range(n-1, -1, -1):
        if arr[i] == key:
            end = i
            break

    return [start, end]

arr = [1, 2, 3, 4, 5, 5]
key = 5

result = findIndex(arr, key)

print(result[0], result[1])
C#
using System;

class Program {
    public static int[] findIndex(int[] arr, int key) {
        int n = arr.Length;
        int start = -1;

        // Step 1: Find first occurrence of key
        for (int i = 0; i < n; i++) {
            if (arr[i] == key) {
                start = i;
                break;
            }
        }

        if (start == -1) {
            return new int[] {-1, -1};
        }

        int end = start;

        // Step 2: Find last occurrence of key
        for (int i = n - 1; i >= 0; i--) {
            if (arr[i] == key) {
                end = i;
                break;
            }
        }

        return new int[] {start, end};
    }

    static void Main() {
        int[] arr = {1, 2, 3, 4, 5, 5};
        int key = 5;
        int[] result = findIndex(arr, key);
        Console.WriteLine(result[0] + " " + result[1]);
    }
}
JavaScript
function findIndex(arr, key) {
    let n = arr.length;
    let start = -1;

    // Step 1: Find first occurrence of key
    for (let i = 0; i < n; i++) {
        if (arr[i] === key) {
            start = i;
            break;
        }
    }

    if (start === -1) {
        return [-1, -1];
    }

    let end = start;

    // Step 2: Find last occurrence of key
    for (let i = n - 1; i >= 0; i--) {
        if (arr[i] === key) {
            end = i;
            break;
        }
    }

    return [start, end];
}

let arr = [1, 2, 3, 4, 5, 5];
let key = 5;

let result = findIndex(arr, key);

console.log(result[0] + ' ' + result[1]);
PHP
<?php
function findIndex($arr, $key) {
    $n = count($arr);
    $start = -1;

    // Step 1: Find first occurrence of key
    for ($i = 0; $i < $n; $i++) {
        if ($arr[$i] == $key) {
            $start = $i;
            break;
        }
    }

    if ($start == -1) {
        return [-1, -1];
    }

    $end = $start;

    // Step 2: Find last occurrence of key
    for ($i = $n - 1; $i >= 0; $i--) {
        if ($arr[$i] == $key) {
            $end = $i;
            break;
        }
    }

    return [$start, $end];
}

$arr = [1, 2, 3, 4, 5, 5];
$key = 5;

$result = findIndex($arr, $key);

echo $result[0]. ''. $result[1] . "\n";
?>

Output
Start index: 3
Last index: 10

Time Complexity: The worst-case time complexity is O(n) because we may need to traverse the entire array. The best case is O(1) when both loops terminate immediately (i.e., the key is found at the beginning and end), where N represents the size of the given array. and best case time complexity will be O(1), when start index is '0' and last index is 'n - 1'.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Related Article: 
Find first and last occurrences of an element in a sorted array

 

Comment