Given an array arr[] of size N, the task is to check if the array contains only one distinct element or not. If it contains only one distinct element then print “Yes”, otherwise print “No”.
Examples:
Input: arr[] = {3, 3, 4, 3, 3}
Output: No
Explanation:
There are 2 distinct elements present in the array {3, 4}.
Therefore, the output is No.
Input: arr[] = {9, 9, 9, 9, 9, 9, 9}
Output: Yes
Explanation:
The only distinct element in the array is 9.
Therefore, the output is Yes.
Naive Approach: The idea is to sort the given array and then for each valid index check if the current element and the next element are the same or not. If they are not the same it means the array contains more than one distinct element, therefore print “No”, otherwise print “Yes”.
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
Better Approach: This problem can be solved by using a set data structure. Since in set, no repetitions are allowed. Below are the steps:
- Insert elements of the array into the set.
- If there is only one distinct element then the size of the set after step 1 will be 1, so print “Yes”.
- Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void uniqueElement( int arr[], int n)
{
unordered_set< int > set;
for ( int i = 0; i < n; i++)
{
set.insert(arr[i]);
}
if (set.size() == 1)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
int main()
{
int arr[] = { 9, 9, 9, 9, 9, 9, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
uniqueElement(arr,n);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void
uniqueElement( int arr[])
{
Set<Integer> set = new HashSet<>();
for ( int i = 0 ; i < arr.length; i++) {
set.add(arr[i]);
}
if (set.size() == 1 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String args[])
{
int arr[] = { 9 , 9 , 9 , 9 , 9 , 9 , 9 };
uniqueElement(arr);
}
}
|
Python3
def uniqueElement(arr, n):
s = set (arr)
if ( len (s) = = 1 ):
print ( 'YES' )
else :
print ( 'NO' )
if __name__ = = '__main__' :
arr = [ 9 , 9 , 9 , 9 , 9 , 9 , 9 ]
n = len (arr)
uniqueElement(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void uniqueElement( int []arr)
{
HashSet< int > set = new HashSet< int >();
for ( int i = 0; i < arr.Length; i++)
{
set .Add(arr[i]);
}
if ( set .Count == 1)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
public static void Main(String []args)
{
int []arr = { 9, 9, 9, 9, 9, 9, 9 };
uniqueElement(arr);
}
}
|
Javascript
<script>
function uniqueElement(arr,n)
{
var set = new Set();
for ( var i = 0; i < n; i++)
{
set.add(arr[i]);
}
if (set.size == 1)
{
document.write( "YES" );
}
else
{
document.write( "NO" );
}
}
var arr = [9, 9, 9, 9, 9, 9, 9];
var n = arr.length;
uniqueElement(arr,n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach: This problem can also be solved without using any extra space. Below are the steps:
- Assume the first element of the array to be the only unique element in the array and store its value in a variable say X.
- Then traverse the array and check if the current element is equal to X or not.
- If found to be true, then keep checking for all array elements. If no element is found to be different from X, print “Yes”.
- Otherwise, if any of the array elements is not equal to X, it means that the array contains more than one unique element. Hence, print “No”.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void uniqueElement( int arr[], int n)
{
int x = arr[0];
int flag = 1;
for ( int i = 0; i < n; i++)
{
if (arr[i] != x)
{
flag = 0;
break ;
}
}
if (flag == 1)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int arr[] = {9, 9, 9,
9, 9, 9, 9};
int n = sizeof (arr) /
sizeof (arr[0]);
uniqueElement(arr, n);
}
|
Java
import java.util.*;
public class Main {
public static void
uniqueElement( int arr[])
{
int x = arr[ 0 ];
int flag = 1 ;
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] != x) {
flag = 0 ;
break ;
}
}
if (flag == 1 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String args[])
{
int arr[] = { 9 , 9 , 9 , 9 , 9 , 9 , 9 };
uniqueElement(arr);
}
}
|
Python3
def uniqueElement(arr):
x = arr[ 0 ]
flag = 1
for i in range ( len (arr)):
if (arr[i] ! = x):
flag = 0
break
if (flag = = 1 ):
print ( "Yes" )
else :
print ( "No" )
arr = [ 9 , 9 , 9 , 9 , 9 , 9 , 9 ]
uniqueElement(arr)
|
C#
using System;
class GFG{
public static void uniqueElement( int []arr)
{
int x = arr[0];
int flag = 1;
for ( int i = 0; i < arr.Length; i++)
{
if (arr[i] != x)
{
flag = 0;
break ;
}
}
if (flag == 1)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
static public void Main ()
{
int []arr = { 9, 9, 9, 9, 9, 9, 9 };
uniqueElement(arr);
}
}
|
Javascript
<script>
function uniqueElement(arr)
{
var x = arr[0];
var flag = 1;
for ( var i = 0; i < arr.length; i++)
{
if (arr[i] != x)
{
flag = 0;
break ;
}
}
if (flag == 1)
document.write( "Yes" );
else
document.write( "No" );
}
var arr = [ 9, 9, 9, 9, 9, 9, 9 ];
uniqueElement(arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)