📌  相关文章
📜  检查所有数组元素是否不同

📅  最后修改于: 2021-10-27 07:33:53             🧑  作者: Mango

给定一个数组,检查数组中的所有元素是否都不同。
例子:

Input : 1, 3, 2, 4
Output : Yes

Input : "Geeks", "for", "Geeks"
Output : No

Input : "All", "Not", "Equal"
Output : Yes

一种简单的解决方案是使用两个嵌套循环。对于每个元素,检查它是否重复。如果任何元素重复,则返回 false。如果没有元素重复,则返回 false。
一个有效的解决方案是散列。我们将所有数组元素放在一个 HashSet 中。如果 HashSet 的大小与数组大小保持一致,则返回 true。

C++
// C++ program to check if all array
// elements are distinct
#include 
using namespace std;
 
bool areDistinct(vector arr)
{
    int n = arr.size();
 
    // Put all array elements in a map
    unordered_set s;
    for (int i = 0; i < n; i++) {
        s.insert(arr[i]);
    }
 
    // If all elements are distinct, size of
    // set should be same array.
    return (s.size() == arr.size());
}
 
// Driver code
int main()
{
    std::vector arr = { 1, 2, 3, 2 };
 
    if (areDistinct(arr)) {
        cout << "All Elements are Distinct";
    }
    else {
        cout << "Not all Elements are Distinct";
    }
 
    return 0;
}


Java
// Java program to check if all array elements are
// distinct or not.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
 
public class DistinctElements {
    public static boolean areDistinct(Integer arr[])
    {
        // Put all array elements in a HashSet
        Set s =
           new HashSet(Arrays.asList(arr));
 
        // If all elements are distinct, size of
        // HashSet should be same array.
        return (s.size() == arr.length);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        Integer[] arr = { 1, 2, 3, 2 };
        if (areDistinct(arr))
            System.out.println("All Elements are Distinct");
        else
            System.out.println("Not all Elements are Distinct");
    }
}


Python3
# Python3 program to check if all array
# elements are distinct
def areDistinct(arr) :
 
    n = len(arr)
 
    # Put all array elements in a map
    s = set()
    for i in range(0, n):
        s.add(arr[i])
     
    # If all elements are distinct,
    # size of set should be same array.
    return (len(s) == len(arr))
 
# Driver code
arr = [ 1, 2, 3, 2 ]
 
if (areDistinct(arr)):
    print("All Elements are Distinct")
 
else :
    print("Not all Elements are Distinct")
 
# This code is contributed by ihritik


C#
// C# program to check if all array elements are
// distinct or not.
using System;
using System.Collections.Generic;
 
public class DistinctElements
{
    public static bool areDistinct(int []arr)
    {
        // Put all array elements in a HashSet
        HashSet s = new HashSet(arr);
 
        // If all elements are distinct, size of
        // HashSet should be same array.
        return (s.Count == arr.Length);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int[] arr = { 1, 2, 3, 2 };
        if (areDistinct(arr))
            Console.WriteLine("All Elements are Distinct");
        else
            Console.WriteLine("Not all Elements are Distinct");
    }
}
 
// This code has been contributed by 29AjayKumar


Javascript


输出:
Not all Elements are Distinct

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程