Find the only different element in an array
Given an array of integers where all elements are same except one element, find the only different element in the array. It may be assumed that the size of the array is at least two.
Examples:
Input : arr[] = {10, 10, 10, 20, 10, 10}
Output : 3
arr[3] is the only different element.
Input : arr[] = {30, 10, 30, 30, 30}
Output : 1
arr[1] is the only different element.
A simple solution is to traverse the array. For every element, check if it is different from others or not. The time complexity of this solution would be O(n*n)
A better solution is to use hashing. We count frequencies of all elements. The hash table will have two elements. We print the element with value (or frequency) equal to 1. This solution works in O(n) time but requires O(n) extra space.
An efficient solution is to first check the first three elements. There can be two cases,
- Two elements are same, i.e., one is different according to given conditions in the question. In this case, the different element is among the first three, so we return the different element.
- All three elements are same. In this case, the different element lies in the remaining array. So we traverse the array from the fourth element and simply check if the value of the current element is different from previous or not.
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int findTheOnlyDifferent( int arr[], int n)
{
if (n == 1)
return -1;
if (n == 2)
return 0;
if (arr[0] == arr[1] && arr[0] != arr[2])
return 2;
if (arr[0] == arr[2] && arr[0] != arr[1])
return 1;
if (arr[1] == arr[2] && arr[0] != arr[1])
return 0;
for ( int i = 3; i < n; i++)
if (arr[i] != arr[i - 1])
return i;
return -1;
}
int main()
{
int arr[] = { 10, 20, 20, 20, 20 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findTheOnlyDifferent(arr, n);
return 0;
}
|
Java
public class GFG
{
static int findTheOnlyDifferent( int [] arr, int n)
{
if (n == 1 )
return - 1 ;
if (n == 2 )
return 0 ;
if (arr[ 0 ] == arr[ 1 ] && arr[ 0 ] != arr[ 2 ])
return 2 ;
if (arr[ 0 ] == arr[ 2 ] && arr[ 0 ] != arr[ 1 ])
return 1 ;
if (arr[ 1 ] == arr[ 2 ] && arr[ 0 ] != arr[ 1 ])
return 0 ;
for ( int i = 3 ; i < n; i++)
if (arr[i] != arr[i - 1 ])
return i;
return - 1 ;
}
public static void main(String args[])
{
int [] arr = { 10 , 20 , 20 , 20 , 20 };
int n = arr.length;
System.out.println(findTheOnlyDifferent(arr, n));
}
}
|
Python3
def findTheOnlyDifferent(arr, n):
if n = = 1 :
return - 1
if n = = 2 :
return 0
if arr[ 0 ] = = arr[ 1 ] and arr[ 0 ] ! = arr[ 2 ]:
return 2
if arr[ 0 ] = = arr[ 2 ] and arr[ 0 ] ! = arr[ 1 ]:
return 1
if arr[ 1 ] = = arr[ 2 ] and arr[ 0 ] ! = arr[ 1 ]:
return 0
for i in range ( 3 , n):
if arr[i] ! = arr[i - 1 ]:
return i
return - 1
if __name__ = = "__main__" :
arr = [ 10 , 20 , 20 , 20 , 20 ]
n = len (arr)
print (findTheOnlyDifferent(arr, n))
|
Javascript
function findTheOnlyDifferent(arr, n) {
if (n === 1)
return -1;
if (n === 2)
return 0;
if (arr[0] === arr[1] && arr[0] !== arr[2])
return 2;
if (arr[0] === arr[2] && arr[0] !== arr[1])
return 1;
if (arr[1] === arr[2] && arr[0] !== arr[1])
return 0;
for (let i = 3; i < n; i++)
if (arr[i] !== arr[i - 1])
return i;
return -1;
}
const arr = [10, 20, 20, 20, 20];
const n = arr.length;
console.log(findTheOnlyDifferent(arr, n));
|
C#
using System;
class GFG
{
static int findTheOnlyDifferent( int [] arr, int n)
{
if (n == 1)
return -1;
if (n == 2)
return 0;
if (arr[0] == arr[1] && arr[0] != arr[2])
return 2;
if (arr[0] == arr[2] && arr[0] != arr[1])
return 1;
if (arr[1] == arr[2] && arr[0] != arr[1])
return 0;
for ( int i = 3; i < n; i++)
if (arr[i] != arr[i - 1])
return i;
return -1;
}
public static void Main()
{
int [] arr = { 10, 20, 20, 20, 20 };
int n = arr.Length;
Console.Write(findTheOnlyDifferent(arr, n));
}
}
|
PHP
<?php
function findTheOnlyDifferent( $arr , $n )
{
if ( $n == 1)
return -1;
if ( $n == 2)
return 0;
if ( $arr [0] == $arr [1] && $arr [0] != $arr [2])
return 2;
if ( $arr [0] == $arr [2] && $arr [0] != $arr [1])
return 1;
if ( $arr [1] == $arr [2] && $arr [0] != $arr [1])
return 0;
for ( $i = 3; $i < $n ; $i ++)
if ( $arr [ $i ] != $arr [ $i - 1])
return $i ;
return -1;
}
$arr = array (10, 20, 20, 20, 20);
$n = sizeof( $arr );
echo findTheOnlyDifferent( $arr , $n );
?>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
SECOND APPROACH : Using STL
Another approach to this problem is by using vector method . Like we will simply copy all the element in another array and then we will simply compare the first element of new sorted array with previous one and the index which is not same will be our answer .
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int arr[] = { 10, 20, 20, 20, 20 };
int n = sizeof (arr) / sizeof (arr[0]);
int arr2[n];
for ( int i=0;i<n;i++)
{
arr2[i]=arr[i];
}
sort(arr2,arr2+n);
for ( int i = 0; i < n; i++) {
if (arr[i] != arr2[1]) {
cout << i << "\n" ;
}
}
}
int main() {
solve();
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
solve();
}
static void solve() {
int [] arr = { 10 , 20 , 20 , 20 , 20 };
int n = arr.length;
int [] arr2 = Arrays.copyOf(arr, n);
Arrays.sort(arr2);
for ( int i = 0 ; i < n; i++) {
if (arr[i] != arr2[ 1 ]) {
System.out.println(i);
}
}
}
}
|
Python3
def solve():
arr = [ 10 , 20 , 20 , 20 , 20 ]
n = len (arr)
arr2 = arr.copy()
arr2.sort()
for i in range (n):
if arr[i] ! = arr2[ 1 ]:
print (i)
solve()
|
Javascript
function findSecondSmallestIndex(arr) {
let n = arr.length;
let arr2 = [...arr];
arr2.sort((a, b) => a - b);
for (let i = 0; i < n; i++) {
if (arr[i] !== arr2[1]) {
return i;
}
}
}
let arr = [10, 20, 20, 20, 20];
let index = findSecondSmallestIndex(arr);
console.log(` ${index}`);
|
C#
using System;
class MainClass {
public static void Main( string [] args)
{
int [] arr = { 10, 20, 20, 20, 20 };
int n = arr.Length;
int [] arr2 = new int [n];
Array.Copy(arr, arr2, n);
Array.Sort(arr2);
for ( int i = 0; i < n; i++) {
if (arr[i] != arr2[1]) {
Console.WriteLine(i);
}
}
}
}
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
26 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...