Algorithm to check if two unsorted integer arrays have the same elements?

倾然丶 夕夏残阳落幕 提交于 2020-01-16 13:20:13

问题


I'm still a newbie when it comes to programming in C so please be thorough in explanations. Also, this is a homework assignment so I would love it if you could help me solve this problem. I've looked around the site and could only find posts similar to this question. The ones that I did find mentioned things like hash tables, which I did not learn so I doubt those kinds of things can be used.

For my assignment, I have two unsorted integer arrays of the same length. They can only have integers in the range of 0-99. Duplicate elements are allowed. The goal is to see whether they have the same elements, therefore checking if they are equal, without sorting them. My professor also said that it has to run in linear time, so sorting them would not be an option.

For example,

A[4]={1,2,3,4}
B[4]={4,3,2,1}

These arrays would be equal. However,

A[3]={1,1,2}
B[3]={2,2,1}

These arrays are not equal.

The algorithm that I came up with goes like this:
The first for loop iterates through the first array and the inner for loop iterates through the second array. It would check to see if the current element in the first array is equal to an element in the second. If they are equal, then the outer loop iterates and the inner for loop starts at the first element again, checking to see if the current element in the first array ever matches with an element in the second. If not, then it exits out of both loops and returns 0 indicating that they are not equal. This algorithm does not run in linear time. Also, it does not check for duplicates since the inner for loop checks all the elements in the second array each time.

I was thinking of changing the value of the element in the second array to something extremely large every time it was found to be equal to an element in the first array. I think that way it would get rid of the duplicate problem. But I still need help making a faster algorithm and understanding my error. Thanks.


回答1:


Simply rethink your task: you have to find out if one array is a permutation of another. To solve this you have only to check how many zeros, ones, ..., 99s in each array. That is,

  • let new array int count[100] be initialized to all zeros
  • for each i: increment count[a[i]] and decrement count[b[i]]
  • if count[] consists of all zeroes then a[] and b[] are permutations.



回答2:


A good and simple trick to sort that out is :

  • sort array A (in (O(n log n))
  • sort array B (in O(n log n))
  • compare both arrays (in O(n))
  • the result being all in all O(n log n)




    回答3:


    Three things to watch : sum of elements of array 1 and array 2,multiplication of
    elements of array 1 and array 2,and their zero count.if all is satisfied,then they are equal. #include

    int main(void)
    {
        int A[4] = { 4 , 3 , 2 , 1 }; 
    
        int B[4] = { 6 , 2 , 1 , 1 };
    
        int varA = 1 , varB = 1;
    
        int sumA = 0 , sumB = 0;
    
        int zeroCountA = 0 , zeroCountB = 0; 
    
        for( int n = 0 ; n < 4 ; n++ )
        {
            if( A[n] != 0)
            {
                varA *= A[n];
                sumA += A[n];
            }
            else
            {
                zeroCountA++;
            }
        }
    
        for( int n = 0 ; n < 4 ; n++ )
        {
            if( B[n] != 0)
            {
                varB *= B[n];
                sumB += B[n];
            }
            else
            {
                zeroCountB++;
            }
        }
    
        if( sumA == sumB )
        {
            if( varA == varB )
            {
                if( zeroCountA == zeroCountB )
                    printf("A = B\n");
            }
            else
            {
                printf("A != B\n");
            }
        }
        else
        {
            printf("A != B\n");
        }
    }
    


    来源:https://stackoverflow.com/questions/33068561/algorithm-to-check-if-two-unsorted-integer-arrays-have-the-same-elements

    标签
    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!