如何检查数组中的所有值是否不同

C++ How to check if all values in array are different?

本文关键字:是否 何检查 检查 数组      更新时间:2023-10-16

你好,我试图检查在任何大小的数组中输入的任何值都不同。我试图使用此代码的嵌套循环,但无法得到一个适当的if语句来检查数组中的每个值是否不同。我很感激任何帮助!

for (unsigned i = 0; i < size; i++)
    for (unsigned k = i + 1; k < size; k++)
        if (arr[i] == arr[k]){
            return false;
        }
return true;

好的,谢谢你们的帮助,你们的建议起作用了!

可以先排序arr吗?

std::sort(std::begin(arr), std::end(arr));
auto pos = std::adjacent_find(std::begin(arr), std::end(arr));
if (pos != std::end(arr))
    // we have a duplicate

第一个for循环错误。这里是j而不是i

for (unsigned i = 0; i < size; i++)
 ...

这是一个方法。

//Implement an algorithm “Unique” to check if all elements of a given set of
//integers are distinct.
#include <iostream>
using namespace std;

int main()
{
    int arr[10] = {10, 20, 50, 90, 30, 60, 35, 40, 85, 90};
    int i, k, origVal = 0, newVal = 0;

    for (i = 0; i < 10; i++)
    {
        origVal = arr[i];
        for (k = i+1; k < 10; k++)
        {
            if (origVal == arr[k])
            {
                newVal = 1;
                break;
            }
        }
        if (newVal ){break;}
    }
    if (newVal == 1)
    {
        cout<<"The Array does not contain completely distinct values"<<endl;
    }
    else 
    {
        cout<< "The Array is distinct"<<endl;
    }
system("PAUSE");
return 0;
}

你的代码有两个问题:

 1. first loop should have j instead if i.
2. The second loop should start from i+1.

std::set只包含唯一的元素。

#include <iostream>
#include <set>
int main()
{
    int a[] = { 1, 9, 4, 5, 8, 3, 1, 3, 5 };
    int b[] = { 1, 9, 4, 5, 8, 3 };
    std::set<int> sa(a, a + 9);
    std::cout << std::boolalpha << (sa.size() == (sizeof(a)/sizeof(*a))); //all values not different
    std::set<int> sb(b, b + 6);
    std::cout << std::boolalpha << (sb.size() == (sizeof(b)/sizeof(*b))); //true, all values are different
}

一个更快更好的方法是使用map。例如

std::map<int, int> m;
for (int i = 0; i < size; ++i) {
    if (m[i] > 0) return false;
    m[i]++;
}
return true;

一行代码看起来像这样

return std::unique(arr, arr + size) == (arr + size);

如果你的序列已经排序了,你想去掉重复的部分,或者你有能力对它或它的一个副本进行排序,然后去掉重复的部分,那么你可以使用std::unique标准库算法。

在http://en.cppreference.com/w/cpp/algorithm/unique上有很好的例子,但您只需要记住,如果将一个没有重复项的排序序列传递给std::unique,它将返回一个指向最后一个元素的迭代器——通常是end()。

如果返回其他内容,则从返回的迭代器开始有重复项,这意味着它们被移动到之后的非重复项排序序列。然后,您可以删除这些,例如,对于std::vector,您执行v.r ease (returnedIterator, v.p end())。

为什么不使用惯用的c++结构呢?

if (std::equal(a1, a1 + sizeof a1 / sizeof *a1, a2))

其中a1和a2是你的两个数组。

我看到问题得到了纠正,只有I而不是I和j,这让我认为这是两个数组。我将把这个答案作为在c++ 11之前版本中使用std::equal的参考。但对于C+11,请参阅下面的Jens解决方案。因为它必须包含<algorithm><iterator>

std::sort( std::begin(arr), std::end(arr) );
auto u = std::unique( std::begin(arr), std::end(arr) );
bool containsDuplicate = u != std::end(arr);
#include<iostream>
#include<algorithm>
int main(){
    int arr[] = {3, 2, 3, 4, 1, 5, 5, 5};
    int len = sizeof(arr) / sizeof(*arr); // Finding length of array
    std::sort(arr, arr+len);
    int unique_elements = std::unique(arr, arr+len) - arr; // Finding number of unique elements
    if(unique_elements == 1) std:: cout << "All elements of this array are Equaln";
    else std::cout << "All elements of this array are not Equaln";
    return 0;
}