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,

  1. 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.
  2. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the only different
// element in an array.
#include <bits/stdc++.h>
using namespace std;
  
// Function to find minimum range
// increments to sort an array
int findTheOnlyDifferent(int arr[], int n)
{
    // Array size must be at least two.
    if (n == 1)
        return -1;
  
    // If there are two elements, then we
    // can return any one element as different
    if (n == 2)
        return 0;
  
    // Check if the different element is among
    // first three
    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;
  
    // If we reach here, then first three elements
    // must be same (assuming that only one element
    // is different.
    for (int i = 3; i < n; i++)
        if (arr[i] != arr[i - 1])
            return i;
  
    return -1;
}
  
// Driver Code
int main()
{
    int arr[] = { 10, 20, 20, 20, 20 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findTheOnlyDifferent(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the only different
// element in an array.
  
public class GFG
{
// Function to find minimum range
// increments to sort an array
static int findTheOnlyDifferent(int[] arr, int n)
{
    // Array size must be at least two.
    if (n == 1)
        return -1;
  
    // If there are two elements, then we
    // can return any one element as different
    if (n == 2)
        return 0;
  
    // Check if the different element is among
    // first three
    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;
  
    // If we reach here, then first three elements
    // must be same (assuming that only one element
    // is different.
    for (int i = 3; i < n; i++)
        if (arr[i] != arr[i - 1])
            return i;
  
    return -1;
}
  
// Driver Code
public static void main(String args[])
{
    int[] arr = { 10, 20, 20, 20, 20 };
    int n = arr.length;
    System.out.println(findTheOnlyDifferent(arr, n));
}
  
// This code is contributed
// by Ryuga
  
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the only 
# different element in an array. 
  
# Function to find minimum range 
# increments to sort an array 
def findTheOnlyDifferent(arr, n): 
  
    # Array size must be at least two. 
    if n == 1
        return -1
  
    # If there are two elements, 
    # then we can return any one 
    # element as different 
    if n == 2
        return 0
  
    # Check if the different element 
    # is among first three 
    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
  
    # If we reach here, then first 
    # three elements must be same 
    # (assuming that only one element 
    # is different. 
    for i in range(3, n): 
        if arr[i] != arr[i - 1]: 
            return
  
    return -1
  
# Driver Code
if __name__ == "__main__":
      
    arr = [10, 20, 20, 20, 20
    n = len(arr) 
    print(findTheOnlyDifferent(arr, n)) 
  
# This code is contributed 
# by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the only different
// element in an array.
using System;
  
class GFG
{
// Function to find minimum range
// increments to sort an array
static int findTheOnlyDifferent(int[] arr, int n)
{
    // Array size must be at least two.
    if (n == 1)
        return -1;
  
    // If there are two elements, then we
    // can return any one element as different
    if (n == 2)
        return 0;
  
    // Check if the different element is among
    // first three
    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;
  
    // If we reach here, then first three elements
    // must be same (assuming that only one element
    // is different.
    for (int i = 3; i < n; i++)
        if (arr[i] != arr[i - 1])
            return i;
  
    return -1;
}
  
// Driver Code
public static void Main()
{
    int[] arr = { 10, 20, 20, 20, 20 };
    int n = arr.Length;
    Console.Write(findTheOnlyDifferent(arr, n));
}
}
  
// This code is contributed
// by Akanksha Rai

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the only different
// element in an array.
  
// Function to find minimum range
// increments to sort an array
function findTheOnlyDifferent($arr, $n)
{
    // Array size must be at least two.
    if ($n == 1)
        return -1;
  
    // If there are two elements, then we
    // can return any one element as different
    if ($n == 2)
        return 0;
  
    // Check if the different element is among
    // first three
    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;
  
    // If we reach here, then first three 
    // elements must be same (assuming that 
    // only one element is different.
    for ($i = 3; $i < $n; $i++)
        if ($arr[$i] != $arr[$i - 1])
            return $i;
  
    return -1;
}
  
// Driver Code
$arr = array(10, 20, 20, 20, 20);
$n = sizeof($arr);
echo findTheOnlyDifferent($arr, $n);
  
// This code is contributed by ajit
?>

chevron_right


Output:

0

Time Complexity: O(n)
Auxiliary Space: O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.