简体   繁体   English

检查两个 arrays 是否相等

[英]Check if two arrays are equal or not

I am trying to know if the two given arrays are equal or not, irrespective of permutation of elements but contains the same elements and frequency of all the elements must be same.我想知道给定的两个 arrays 是否相等,无论元素的排列如何,但包含相同的元素并且所有元素的频率必须相同。

    int SameArray(int arr1[], int arr2[], int N, int M)
    {
        unordered_map<int, int> ump;
        if(N == M)
        {
            for(int i = 0; i < N; i++)
            {
                ump[arr1[i]]++;
            }
            for(int i = 0; i< M; i++)
            {
                if(ump.find(arr2[i]) != ump.end())
                    ump[arr2[i]]--;
            }
            if(ump.empty())
            return 1;
        }
        return 0;
    }

it's not showing any errors but output is always 0.它没有显示任何错误,但 output 始终为 0。

You're looking for std::is_permutation :您正在寻找std::is_permutation

bool SameArray(std::vector<int> arr1, std::vector<int> arr2) {
    return std::is_permutation(arr1.begin(), arr1.end(), arr2.begin(), arr2.end());
}

I took the liberty of changing your function return to bool and taking std::vector s as function parameters since this is C++ and not C. I took the liberty of changing your function return to bool and taking std::vector s as function parameters since this is C++ and not C.

If you're curious about how std::permutation 's comparasion works, look at its example implementation .如果您对std::permutation的比较如何工作感到好奇,请查看其示例实现

The condition in the if statement if 语句中的条件

if(ump.empty())

is not correct.是不正确的。 The map can not be empty provided that the passed arrays do not have zero sizes.如果传递的 arrays 的大小不为零,则 map 不能为空。

Instead of the condition you could use the standard algorithm std::all_of .您可以使用标准算法std::all_of代替条件。 Also there is no sense to pass the two sizes of the arrays because if they are not equal to each other then it is evident that the arrays are not equal each other.此外,通过 arrays 的两种尺寸是没有意义的,因为如果它们彼此不相等,那么很明显 arrays 彼此不相等。

Also the array parameters shall be specified with the qualifier const because they are not changed in the function.此外,数组参数应使用限定符 const 指定,因为它们在 function 中没有更改。

Here is a demonstrative program that shows how the function can be defined.这是一个演示程序,展示了如何定义 function。

#include <iostream>
#include <iomanip>
#include <unordered_map>
#include <iterator>
#include <algorithm>

bool SameArray( const int a1[], const int a2[], size_t n )
{
    sstd::unordered_map<int, int> m;

    for ( const int *p = a1; p != a1 + n; ++p ) ++m[*p];
    for ( const int *p = a2; p != a2 + n; ++p ) --m[*p];


    return std::all_of( std::begin( m ), std::end( m ), 
                        []( const auto &p) { return p.second == 0; } );
}

int main() 
{
    const size_t N = 20;

    int a1[N] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
    int a2[N] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    std::cout << std::boolalpha << SameArray( a1, a2, N ) << '\n';

    return 0;
}

Its output is它的 output 是

true

You need to check if every key in the map has a value of zero.您需要检查 map 中的每个键的值是否为零。 Instead of ump.empty() you can do the below code.而不是ump.empty()您可以执行以下代码。

for (auto& it: ump) {
    if(it.second != 0) {
        return 0;
} 
return 1;

ump[arr2[i]]--; is not going to delete the key.不会删除密钥。 You have to check whether the value of each entry is zero or not.您必须检查每个条目的值是否为零。 I have added below statement before return 1 -我在return 1之前添加了以下语句 -

for (auto it = ump.begin(); it.= ump;end(); ++it ) if(it->second != 0) return 0;

int SameArray(int arr1[], int arr2[], int N, int M)
{
    unordered_map<int, int> ump;
    if(N == M)
    {
        for(int i = 0; i < N; i++)
        {
            ump[arr1[i]]++;
        }
        for(int i = 0; i< M; i++)
        {
            if(ump.find(arr2[i]) != ump.end())
                ump[arr2[i]]--;
        }
        for (auto it = ump.begin(); it != ump.end(); ++it ) if(it->second != 0) return 0; 
        return 1;
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM