Find index of an extra element present in one sorted array
Last Updated :
23 Jul, 2025
Given two sorted arrays. There is only 1 difference between the arrays. The first array has one element extra added in between. Find the index of the extra element.
Examples:
Input: {2, 4, 6, 8, 9, 10, 12};
{2, 4, 6, 8, 10, 12};
Output: 4
Explanation: The first array has an extra element 9.
The extra element is present at index 4.
Input: {3, 5, 7, 9, 11, 13}
{3, 5, 7, 11, 13}
Output: 3
Explanation: The first array has an extra element 9.
The extra element is present at index 3.
Method 1: This includes the basic approach to solve this particular problem.
Approach: The basic method is to iterate through the whole second array and check element by element if they are different. As the array is sorted, checking the adjacent position of two arrays should be similar until and unless the missing element is found.
Algorithm:
- Traverse through the array from start to end.
- Check if the element at i'th element of the two arrays is similar or not.
- If the elements are not similar then print the index and break
Implementation:
C++
// C++ program to find an extra
// element present in arr1[]
#include <iostream>
using namespace std;
// Returns index of extra element
// in arr1[]. n is size of arr2[].
// Size of arr1[] is n-1.
int findExtra(int arr1[],
int arr2[], int n)
{
for (int i = 0; i < n; i++)
if (arr1[i] != arr2[i])
return i;
return n;
}
// Driver code
int main()
{
int arr1[] = {2, 4, 6, 8,
10, 12, 13};
int arr2[] = {2, 4, 6,
8, 10, 12};
int n = sizeof(arr2) / sizeof(arr2[0]);
// Solve is passed both arrays
cout << findExtra(arr1, arr2, n);
return 0;
}
Java
// Java program to find an extra
// element present in arr1[]
class GFG
{
// Returns index of extra element
// in arr1[]. n is size of arr2[].
// Size of arr1[] is n-1.
static int findExtra(int arr1[],
int arr2[], int n)
{
for (int i = 0; i < n; i++)
if (arr1[i] != arr2[i])
return i;
return n;
}
// Driver Code
public static void main (String[] args)
{
int arr1[] = {2, 4, 6, 8,
10, 12, 13};
int arr2[] = {2, 4, 6,
8, 10, 12};
int n = arr2.length;
// Solve is passed both arrays
System.out.println(findExtra(arr1,
arr2, n));
}
}
// This code is contributed by Harsh Agarwal
Python3
# Python 3 program to find an
# extra element present in arr1[]
# Returns index of extra .
# element in arr1[] n is
# size of arr2[]. Size of
# arr1[] is n-1.
def findExtra(arr1, arr2, n) :
for i in range(0, n) :
if (arr1[i] != arr2[i]) :
return i
return n
# Driver code
arr1 = [2, 4, 6, 8, 10, 12, 13]
arr2 = [2, 4, 6, 8, 10, 12]
n = len(arr2)
# Solve is passed both arrays
print(findExtra(arr1, arr2, n))
# This code is contributed
# by Nikita Tiwari.
C#
// C# program to find an extra
// element present in arr1[]
using System;
class GfG
{
// Returns index of extra
// element in arr1[]. n is
// size of arr2[]. Size of
// arr1[] is n-1.
static int findExtra(int []arr1,
int []arr2, int n)
{
for (int i = 0; i < n; i++)
if (arr1[i] != arr2[i])
return i;
return n;
}
// Driver code
public static void Main ()
{
int []arr1 = {2, 4, 6, 8,
10, 12, 13};
int []arr2 = {2, 4, 6,
8, 10, 12};
int n = arr2.Length;
// Solve is passed both arrays
Console.Write(findExtra(arr1, arr2, n));
}
}
// This code is contributed by parashar.
PHP
<?php
// PHP program to find an extra
// element present in arr1[]
// Returns index of extra element
// in arr1[]. n is size of arr2[].
// Size of arr1[] is n-1.
function findExtra($arr1,
$arr2, $n)
{
for ($i = 0; $i < $n; $i++)
if ($arr1[$i] != $arr2[$i])
return $i;
return $n;
}
// Driver code
$arr1 = array (2, 4, 6, 8,
10, 12, 13);
$arr2 = array(2, 4, 6,
8, 10, 12);
$n = sizeof($arr2);
// Solve is passed
// both arrays
echo findExtra($arr1, $arr2, $n);
// This code is contributed by ajit
?>
JavaScript
<script>
// JavaScript program to find an extra
// element present in arr1[]
// Returns index of extra element
// in arr1[]. n is size of arr2[].
// Size of arr1[] is n-1.
function findExtra(arr1, arr2, n)
{
for (let i = 0; i < n; i++)
if (arr1[i] != arr2[i])
return i;
return n;
}
// Driver code
let arr1 = [2, 4, 6, 8,
10, 12, 13];
let arr2 = [2, 4, 6,
8, 10, 12];
let n = arr2.length;
// Solve is passed both arrays
document.write(findExtra(arr1, arr2, n));
// This code is contributed by Surbhi Tyagi.
</script>
Complexity Analysis:
- Time complexity: O(n).
As one traversal through the array is needed, so the time complexity is linear. - Space complexity: O(1).
Since no extra space is required, the time complexity is constant.
Method 2: This method is a better way to solve the above problem and uses the concept of binary search.
Approach:To find the index of the missing element in less than linear time, binary search can be used, the idea is all the indices greater than or equal to the index of the missing element will have different elements in both the arrays and all the indices less than that index will have the similar elements in both arrays.
Algorithm:
- Create three variables, low = 0, high = n-1, mid, ans = n
- Run a loop until low is less than or equal to high, i.e till our search range is less than zero.
- If the mid element, i.e (low + high)/2, of both arrays is similar then update the search to second half of the search range, i.e low = mid + 1
- Else update the search to the first half of the search range, i.e high = mid - 1, and update the answer to the current index, ans = mid
- Print the index.
Implementation:
C++
// C++ program to find an extra
// element present in arr1[]
#include <iostream>
using namespace std;
// Returns index of extra element
// in arr1[]. n is size of arr2[].
// Size of arr1[] is n-1.
int findExtra(int arr1[],
int arr2[], int n)
{
// Initialize result
int index = n;
// left and right are end
// points denoting the current range.
int left = 0, right = n - 1;
while (left <= right)
{
int mid = (left + right) / 2;
// If middle element is same
// of both arrays, it means
// that extra element is after
// mid so we update left to mid+1
if (arr2[mid] == arr1[mid])
left = mid + 1;
// If middle element is different
// of the arrays, it means that
// the index we are searching for
// is either mid, or before mid.
// Hence we update right to mid-1.
else
{
index = mid;
right = mid - 1;
}
}
// when right is greater than
// left our search is complete.
return index;
}
// Driver code
int main()
{
int arr1[] = {2, 4, 6, 8, 10, 12, 13};
int arr2[] = {2, 4, 6, 8, 10, 12};
int n = sizeof(arr2) / sizeof(arr2[0]);
// Solve is passed both arrays
cout << findExtra(arr1, arr2, n);
return 0;
}
Java
// Java program to find an extra
// element present in arr1[]
class GFG
{
// Returns index of extra element
// in arr1[]. n is size of arr2[].
// Size of arr1[] is n-1.
static int findExtra(int arr1[],
int arr2[], int n)
{
// Initialize result
int index = n;
// left and right are end
// points denoting the current range.
int left = 0, right = n - 1;
while (left <= right)
{
int mid = (left+right) / 2;
// If middle element is same
// of both arrays, it means
// that extra element is after
// mid so we update left to mid+1
if (arr2[mid] == arr1[mid])
left = mid + 1;
// If middle element is different
// of the arrays, it means that
// the index we are searching for
// is either mid, or before mid.
// Hence we update right to mid-1.
else
{
index = mid;
right = mid - 1;
}
}
// when right is greater than
// left, our search is complete.
return index;
}
// Driver Code
public static void main (String[] args)
{
int arr1[] = {2, 4, 6, 8, 10, 12,13};
int arr2[] = {2, 4, 6, 8, 10, 12};
int n = arr2.length;
// Solve is passed both arrays
System.out.println(findExtra(arr1, arr2, n));
}
}
// This code is contributed by Harsh Agarwal
Python3
# Python3 program to find an extra
# element present in arr1[]
# Returns index of extra element
# in arr1[]. n is size of arr2[].
# Size of arr1[] is n-1.
def findExtra(arr1, arr2, n) :
index = n # Initialize result
# left and right are end points
# denoting the current range.
left = 0
right = n - 1
while (left <= right) :
mid = (int)((left + right) / 2)
# If middle element is same
# of both arrays, it means
# that extra element is after
# mid so we update left to
# mid + 1
if (arr2[mid] == arr1[mid]) :
left = mid + 1
# If middle element is different
# of the arrays, it means that
# the index we are searching for
# is either mid, or before mid.
# Hence we update right to mid-1.
else :
index = mid
right = mid - 1
# when right is greater than left our
# search is complete.
return index
# Driver code
arr1 = [2, 4, 6, 8, 10, 12, 13]
arr2 = [2, 4, 6, 8, 10, 12]
n = len(arr2)
# Solve is passed both arrays
print(findExtra(arr1, arr2, n))
# This code is contributed by Nikita Tiwari.
C#
// C# program to find an extra
// element present in arr1[]
using System;
class GFG {
// Returns index of extra
// element in arr1[]. n is
// size of arr2[].
// Size of arr1[] is
// n - 1.
static int findExtra(int []arr1,
int []arr2,
int n)
{
// Initialize result
int index = n;
// left and right are
// end points denoting
// the current range.
int left = 0, right = n - 1;
while (left <= right)
{
int mid = (left+right) / 2;
// If middle element is
// same of both arrays,
// it means that extra
// element is after mid
// so we update left
// to mid + 1
if (arr2[mid] == arr1[mid])
left = mid + 1;
// If middle element is
// different of the arrays,
// it means that the index
// we are searching for is
// either mid, or before mid.
// Hence we update right to mid-1.
else
{
index = mid;
right = mid - 1;
}
}
// when right is greater
// than left our
// search is complete.
return index;
}
// Driver Code
public static void Main ()
{
int []arr1 = {2, 4, 6, 8, 10, 12,13};
int []arr2 = {2, 4, 6, 8, 10, 12};
int n = arr2.Length;
// Solve is passed
// both arrays
Console.Write(findExtra(arr1, arr2, n));
}
}
// This code is contributed by nitin mittal.
PHP
<?php
// PHP program to find an extra
// element present in arr1[]
// Returns index of extra element
// in arr1[]. n is size of arr2[].
// Size of arr1[] is n-1.
function findExtra($arr1, $arr2, $n)
{
// Initialize result
$index = $n;
// left and right are
// end points denoting
// the current range.
$left = 0; $right = $n - 1;
while ($left <= $right)
{
$mid = ($left+$right) / 2;
// If middle element is same
// of both arrays, it means
// that extra element is after
// mid so we update left to mid+1
if ($arr2[$mid] == $arr1[$mid])
$left = $mid + 1;
// If middle element is different
// of the arrays, it means that the
// index we are searching for is either
// mid, or before mid. Hence we update
// right to mid-1.
else
{
$index = $mid;
$right = $mid - 1;
}
}
// when right is greater than
// left, our search is complete.
return $index;
}
// Driver code
{
$arr1 = array(2, 4, 6, 8,
10, 12, 13);
$arr2 = array(2, 4, 6,
8, 10, 12);
$n = sizeof($arr2) / sizeof($arr2[0]);
// Solve is passed both arrays
echo findExtra($arr1, $arr2, $n);
return 0;
}
// This code is contributed by nitin mittal
?>
JavaScript
<script>
// Javascript program to find an extra
// element present in arr1[]
// Returns index of extra element
// in arr1[]. n is size of arr2[].
// Size of arr1[] is n-1.
function findExtra( arr1, arr2, n)
{
// Initialize result
let index = n;
// left and right are end
// points denoting the current range.
let left = 0, right = n - 1;
while (left <= right)
{
let mid = Math.floor((left + right) / 2);
// If middle element is same
// of both arrays, it means
// that extra element is after
// mid so we update left to mid+1
if (arr2[mid] == arr1[mid])
left = mid + 1;
// If middle element is different
// of the arrays, it means that
// the index we are searching for
// is either mid, or before mid.
// Hence we update right to mid-1.
else
{
index = mid;
right = mid - 1;
}
}
// when right is greater than
// left our search is complete.
return index;
}
// Driver program
let arr1 = [2, 4, 6, 8, 10, 12, 13];
let arr2 = [2, 4, 6, 8, 10, 12];
let n = arr2.length;
// Solve is passed both arrays
document.write(findExtra(arr1, arr2, n));
</script>
Complexity Analysis:
- Time complexity : O(log n).
The time complexity of binary search is O(log n) - Space complexity : O(1).
As no extra space is required, so the time complexity is constant.
Method 3: This method solves the given problem using the predefined function.
Approach: To find the element which is different, find the sum of each array and subtract the sums and find the absolute value. Search the larger array and check if the absolute is equal to an index and return that index. If an element is missing and all the other elements are the same, then the difference of sums will be equal to missing element.
Algorithm:
- Create a function to calculate the sum of two arrays.
- Find the absolute difference between the sum of two arrays (value).
- Traverse the larger array from start too end
- If the element at any index is equal to value, then print the index and break the loop.
Implementation:
C++
// C++ code for above approach
#include<bits/stdc++.h>
using namespace std;
// function return sum of array elements
int sum(int arr[], int n)
{
int summ = 0;
for (int i = 0; i < n; i++)
{
summ += arr[i];
}
return summ;
}
// function return index of given element
int indexOf(int arr[], int element, int n)
{
for (int i = 0; i < n; i++)
{
if (arr[i] == element)
{
return i;
}
}
return -1;
}
// Function to find Index
int find_extra_element_index(int arrA[],
int arrB[],
int n, int m)
{
// Calculating extra element
int extra_element = sum(arrA, n) -
sum(arrB, m);
// returns index of extra element
return indexOf(arrA, extra_element, n);
}
// Driver Code
int main()
{
int arrA[] = {2, 4, 6, 8, 10, 12, 13};
int arrB[] = {2, 4, 6, 8, 10, 12};
int n = sizeof(arrA) / sizeof(arrA[0]);
int m = sizeof(arrB) / sizeof(arrB[0]);
cout << find_extra_element_index(arrA, arrB, n, m);
}
// This code is contributed by mohit kumar
Java
// Java code for above approach
class GFG
{
// Function to find Index
static int find_extra_element_index(int[] arrA,
int[] arrB)
{
// Calculating extra element
int extra_element = sum(arrA) - sum(arrB);
// returns index of extra element
return indexOf(arrA, extra_element);
}
// function return sum of array elements
static int sum(int[] arr)
{
int sum = 0;
for (int i = 0; i < arr.length; i++)
{
sum += arr[i];
}
return sum;
}
// function return index of given element
static int indexOf(int[] arr, int element)
{
for (int i = 0; i < arr.length; i++)
{
if (arr[i] == element)
{
return i;
}
}
return -1;
}
// Driver Code
public static void main(String[] args)
{
int[] arrA = {2, 4, 6, 8, 10, 12, 13};
int[] arrB = {2, 4, 6, 8, 10, 12};
System.out.println(find_extra_element_index(arrA, arrB));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 code for above approach
# Function to find Index
def find_extra_element_index(arrA, arrB):
# Calculating extra element
extra_element = sum(arrA) - sum(arrB)
# returns index of extra element
return arrA.index(extra_element)
# Driver Code
arrA = [2, 4, 6, 8, 10, 12, 13]
arrB = [2, 4, 6, 8, 10, 12]
print(find_extra_element_index(arrA,arrB))
# This code is contributed by Dravid
C#
// C# code for above approach
using System;
class GFG
{
// Function to find Index
static int find_extra_element_index(int[] arrA,
int[] arrB)
{
// Calculating extra element
int extra_element = sum(arrA) - sum(arrB);
// returns index of extra element
return indexOf(arrA, extra_element);
}
// function return sum of array elements
static int sum(int[] arr)
{
int sum = 0;
for (int i = 0; i < arr.Length; i++)
{
sum += arr[i];
}
return sum;
}
// function return index of given element
static int indexOf(int[] arr, int element)
{
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == element)
{
return i;
}
}
return -1;
}
// Driver Code
public static void Main(String[] args)
{
int[] arrA = {2, 4, 6, 8, 10, 12, 13};
int[] arrB = {2, 4, 6, 8, 10, 12};
Console.WriteLine(find_extra_element_index(arrA, arrB));
}
}
// This code has been contributed by 29AjayKumar
JavaScript
<script>
// Javascript code for above approach
// Function to find Index
function find_extra_element_index(arrA, arrB)
{
// Calculating extra element
let extra_element = sum(arrA) - sum(arrB);
// returns index of extra element
return indexOf(arrA, extra_element);
}
// function return sum of array elements
function sum(arr)
{
let sum = 0;
for (let i = 0; i < arr.length; i++)
{
sum += arr[i];
}
return sum;
}
// function return index of given element
function indexOf(arr, element)
{
for (let i = 0; i < arr.length; i++)
{
if (arr[i] == element)
{
return i;
}
}
return -1;
}
let arrA = [2, 4, 6, 8, 10, 12, 13];
let arrB = [2, 4, 6, 8, 10, 12];
document.write(find_extra_element_index(arrA, arrB));
</script>
Complexity Analysis:
- Time Complexity: O(n).
Since only three traversals through the array is needed, So the time complexity is linear. - Space Complexity: O(1).
As no extra space is required, so the time complexity is constant.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem