简体   繁体   中英

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.

    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.

You're looking for 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.

If you're curious about how std::permutation 's comparasion works, look at its example implementation .

The condition in the if statement

if(ump.empty())

is not correct. The map can not be empty provided that the passed arrays do not have zero sizes.

Instead of the condition you could use the standard algorithm 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.

Also the array parameters shall be specified with the qualifier const because they are not changed in the function.

Here is a demonstrative program that shows how the function can be defined.

#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

true

You need to check if every key in the map has a value of zero. Instead of ump.empty() you can do the below code.

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 -

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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