Find the just distinct element in an array

Given an array of integers where all elements are same except one element, find the just distinct element in the array. It may be assumed that the size of an array is at least two.Examples:

Input: arr[] = {10, 10, 10, 20, 10, 10}
Output: 3
arr[3] is the just distinct element.

Input: arr[] = {30, 10, 30, 30, 30}
Output: 1
arr[1] is the just distinct element.

A simple solution is to traverse the array. For every element, check if it is distinct 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 distinct according to given conditions in the question. In this case, the distinct element is among the first three, so we return the distinct element.
  2. All three elements are same. In this case, the distinct element lies in the remaining array. So, we traverse an array from the fourth element and simply check if the value of the current element is distinct from previous or not.

Below is the implementation of the above idea.

Python3

# Python3 program to find the just
# distinct 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 when distinct
    if n == 2:
        return 0
    # Check if the distinct 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 just one element
    # is different.
    for i in range(3, n):
        if arr[i] != arr[i - 1]:
            return i
    return -1
# Driver Code
if __name__ == "__main__":
    
    arr = [10, 20, 20, 20, 20]
    n = len(arr)
    print(findTheOnlyDifferent(arr, n))
Output
0

SECOND APPROACH: Using STL

Another approach to this problem is by using vector functions/method. similar as we will simply copy all the element in another an array and then we will simply compare the first element of new sorted an array with previous one and the index which is not same will be our answer.

Below is the implementation of the above approach:


Python3

# Python3 program to find the just different
# Function to solve the problem
def solve():
# Initialize an array
arr = [10, 20, 20, 20, 20]
n = len(arr)
# Create a copy of the an array and sort it
arr2 = arr.copy()
arr2.sort()
# Loop through the original an array and print the indices
# of elements that are not equal to the second smallest element
for i in range(n):
if arr[i] != arr2[1]:
print(i)
# Call the solve function
solve()
Output
0

Explore More

Python 10 Programs for Beginners

Program 1:Write a program to create a 2D array using NumPy. Output: Program 2: Write a program to convert a python list to a NumPy array. Output: Program 3: Write a program to create matrix of 3×3 from 11 to 30. Output: Program 4: Write a program to create a data frame to store data of candidates who appeared in

Find index of first occurrence when an unsorted array is sorted

Find index of first occurrence when an unsorted an array is sorted Given an unsorted an array and a number x, find an index of first occurrence of x when

Find sum non repeating distinct elements array

Find sum of non-repeating (distinct) elements in an array Given an integer an array with repeated elements, the task is to find the sum of all distinct elements in the