给定一个大小为N的数组arr[] ,任务是检查该数组是否只包含一个不同的元素。如果它只包含一个不同的元素,则打印“是” ,否则打印“否” 。
例子:
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.
朴素的方法:想法是对给定的数组进行排序,然后针对每个有效索引检查当前元素和下一个元素是否相同。如果它们不相同,则表示该数组包含多个不同的元素,因此打印“ No” ,否则打印“ Yes” 。
时间复杂度: O(N*logN)
辅助空间: O(1)
更好的方法:这个问题可以通过使用集合数据结构来解决。由于在集合中,不允许重复。以下是步骤:
- 将数组的元素插入到集合中。
- 如果只有一个不同的元素,那么第 1 步之后集合的大小将为 1,因此打印“Yes” 。
- 否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find if the array
// contains only one distinct element
void uniqueElement(int arr[],int n)
{
// Create a set
unordered_set set;
// Traversing the array
for(int i = 0; i < n; i++)
{
set.insert(arr[i]);
}
// Compare and print the result
if(set.size() == 1)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
// Driver code
int main()
{
int arr[] = { 9, 9, 9, 9, 9, 9, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
uniqueElement(arr,n);
return 0;
}
// This code is contributed by rutvik_56
Java
// Java program for the above approach
import java.util.*;
public class Main {
// Function to find if the array
// contains only one distinct element
public static void
uniqueElement(int arr[])
{
// Create a set
Set set = new HashSet<>();
// Traversing the array
for (int i = 0; i < arr.length; i++) {
set.add(arr[i]);
}
// Compare and print the result
if (set.size() == 1)
System.out.println("Yes");
else
System.out.println("No");
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 9, 9, 9, 9, 9, 9, 9 };
// Function call
uniqueElement(arr);
}
}
Python3
# Python3 program for the above approach
# Function to find if the array
# contains only one distinct element
def uniqueElement(arr, n):
# Create a set
s = set(arr)
# Compare and print the result
if(len(s) == 1):
print('YES')
else:
print('NO')
# Driver code
if __name__=='__main__':
arr = [ 9, 9, 9, 9, 9, 9, 9 ]
n = len(arr)
# Function call
uniqueElement(arr, n)
# This code is contributed by rutvik_56
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find if the array
// contains only one distinct element
public static void uniqueElement(int []arr)
{
// Create a set
HashSet set = new HashSet();
// Traversing the array
for(int i = 0; i < arr.Length; i++)
{
set.Add(arr[i]);
}
// Compare and print the result
if (set.Count == 1)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
// Driver Code
public static void Main(String []args)
{
int []arr = { 9, 9, 9, 9, 9, 9, 9 };
// Function call
uniqueElement(arr);
}
}
// This code is contributed by Amit Katiyar
Javascript
C++
// C++ program for
// the above approach
#include
using namespace std;
// Function to find if the array
// contains only one distinct element
void uniqueElement(int arr[], int n)
{
// Assume first element to
// be the unique element
int x = arr[0];
int flag = 1;
// Traversing the array
for (int i = 0; i < n; i++)
{
// If current element is not
// equal to X then break the
// loop and print No
if (arr[i] != x)
{
flag = 0;
break;
}
}
// Compare and print the result
if (flag == 1)
cout << "Yes";
else
cout << "No";
}
// Driver Code
int main()
{
int arr[] = {9, 9, 9,
9, 9, 9, 9};
int n = sizeof(arr) /
sizeof(arr[0]);
// Function call
uniqueElement(arr, n);
}
// This code is contributed by Chitranayal
Java
// Java program for the above approach
import java.util.*;
public class Main {
// Function to find if the array
// contains only one distinct element
public static void
uniqueElement(int arr[])
{
// Assume first element to
// be the unique element
int x = arr[0];
int flag = 1;
// Traversing the array
for (int i = 0; i < arr.length; i++) {
// If current element is not
// equal to X then break the
// loop and print No
if (arr[i] != x) {
flag = 0;
break;
}
}
// Compare and print the result
if (flag == 1)
System.out.println("Yes");
else
System.out.println("No");
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 9, 9, 9, 9, 9, 9, 9 };
// Function call
uniqueElement(arr);
}
}
Python3
# Python3 program for the above approach
# Function to find if the array
# contains only one distinct element
def uniqueElement(arr):
# Assume first element to
# be the unique element
x = arr[0]
flag = 1
# Traversing the array
for i in range(len(arr)):
# If current element is not
# equal to X then break the
# loop and print No
if(arr[i] != x):
flag = 0
break
# Compare and print the result
if(flag == 1):
print("Yes")
else:
print("No")
# Driver Code
# Given array arr[]
arr = [ 9, 9, 9, 9, 9, 9, 9 ]
# Function call
uniqueElement(arr)
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
class GFG{
// Function to find if the array
// contains only one distinct element
public static void uniqueElement(int []arr)
{
// Assume first element to
// be the unique element
int x = arr[0];
int flag = 1;
// Traversing the array
for(int i = 0; i < arr.Length; i++)
{
// If current element is not
// equal to X then break the
// loop and print No
if (arr[i] != x)
{
flag = 0;
break;
}
}
// Compare and print the result
if (flag == 1)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
// Driver code
static public void Main ()
{
int []arr = { 9, 9, 9, 9, 9, 9, 9 };
// Function call
uniqueElement(arr);
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(N)
高效的方法:这个问题也可以在不使用任何额外空间的情况下解决。以下是步骤:
- 假设数组的第一个元素是数组中唯一的唯一元素,并将其值存储在一个变量中,比如X 。
- 然后遍历数组并检查当前元素是否等于X。
- 如果发现为真,则继续检查所有数组元素。如果没有发现与X不同的元素,则打印“是”。
- 否则,如果任何数组元素不等于X ,则表示该数组包含多个唯一元素。因此,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for
// the above approach
#include
using namespace std;
// Function to find if the array
// contains only one distinct element
void uniqueElement(int arr[], int n)
{
// Assume first element to
// be the unique element
int x = arr[0];
int flag = 1;
// Traversing the array
for (int i = 0; i < n; i++)
{
// If current element is not
// equal to X then break the
// loop and print No
if (arr[i] != x)
{
flag = 0;
break;
}
}
// Compare and print the result
if (flag == 1)
cout << "Yes";
else
cout << "No";
}
// Driver Code
int main()
{
int arr[] = {9, 9, 9,
9, 9, 9, 9};
int n = sizeof(arr) /
sizeof(arr[0]);
// Function call
uniqueElement(arr, n);
}
// This code is contributed by Chitranayal
Java
// Java program for the above approach
import java.util.*;
public class Main {
// Function to find if the array
// contains only one distinct element
public static void
uniqueElement(int arr[])
{
// Assume first element to
// be the unique element
int x = arr[0];
int flag = 1;
// Traversing the array
for (int i = 0; i < arr.length; i++) {
// If current element is not
// equal to X then break the
// loop and print No
if (arr[i] != x) {
flag = 0;
break;
}
}
// Compare and print the result
if (flag == 1)
System.out.println("Yes");
else
System.out.println("No");
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 9, 9, 9, 9, 9, 9, 9 };
// Function call
uniqueElement(arr);
}
}
蟒蛇3
# Python3 program for the above approach
# Function to find if the array
# contains only one distinct element
def uniqueElement(arr):
# Assume first element to
# be the unique element
x = arr[0]
flag = 1
# Traversing the array
for i in range(len(arr)):
# If current element is not
# equal to X then break the
# loop and print No
if(arr[i] != x):
flag = 0
break
# Compare and print the result
if(flag == 1):
print("Yes")
else:
print("No")
# Driver Code
# Given array arr[]
arr = [ 9, 9, 9, 9, 9, 9, 9 ]
# Function call
uniqueElement(arr)
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
class GFG{
// Function to find if the array
// contains only one distinct element
public static void uniqueElement(int []arr)
{
// Assume first element to
// be the unique element
int x = arr[0];
int flag = 1;
// Traversing the array
for(int i = 0; i < arr.Length; i++)
{
// If current element is not
// equal to X then break the
// loop and print No
if (arr[i] != x)
{
flag = 0;
break;
}
}
// Compare and print the result
if (flag == 1)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
// Driver code
static public void Main ()
{
int []arr = { 9, 9, 9, 9, 9, 9, 9 };
// Function call
uniqueElement(arr);
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)