How to compare two arrays?
#5

Why would you do the checks n * n times?
Further more you should check if they are the same size before iterating through them.

pawn Код:
new bool:are_same;

// If they are the same size
if (sizeof arr1 == sizeof arr2)
{
    // Assume both arrays are the same
    are_same = true;
   
    // Iterate through both arrays
    for (new i; i < sizeof arr1; i++)
    {
        // If both elements are not the same
        if (arr1[i] != arr2[i])
        {
            // Both arrays are not the same
            are_same = false;
           
            // No need to check further elements
            break;
        }
    }
}

// "are_same" is "true" if both arrays are the same, otherwise "false"
It takes O(n) time at worst case.
Reply


Messages In This Thread
How to compare two arrays? - by JustNothing - 29.10.2018, 07:28
Re: How to compare two arrays? - by TheToretto - 29.10.2018, 07:50
Re: How to compare two arrays? - by TheToretto - 29.10.2018, 08:40
Re: How to compare two arrays? - by TheToretto - 29.10.2018, 08:56
Re: How to compare two arrays? - by BigETI - 29.10.2018, 09:44
Re: How to compare two arrays? - by JustNothing - 29.10.2018, 12:22
Re: How to compare two arrays? - by SyS - 29.10.2018, 12:57

Forum Jump:


Users browsing this thread: 1 Guest(s)