开发者

AS3 Comparing 2 Arrays

开发者 https://www.devze.com 2023-03-19 13:33 出处:网络
I\'m having a hard time wrapping my head around this problem. I have 2 unsorted arrays that need to be compared, array2 must contain all elements of array1 and array2 can any number of extra elements

I'm having a hard time wrapping my head around this problem. I have 2 unsorted arrays that need to be compared, array2 must contain all elements of array1 and array2 can any number of extra elements without affecting the result. I don't see any benefits from sorting the arrays and comparing the string values as array2 can have extra information causing noise.

var array1:Array;
var array2:Array;

// array2 must contain all elements of array1.

var array1:Array = new Array(1, 5, 6, 7);
var array2:Array = new Array(5, 9, 6, 4, 7, 8);
// comparing this should return false (array2 is missing and element开发者_运维百科 from array1)

var array1:Array = new Array(1, 5, 6, 7);
var array2:Array = new Array(5, 9, 6, 4, 7, 1);
// comparing this should return true (array2 has all the elements of array1)

Any help in the right direction would be greatly appreciated.


Something like this?

function checkContent($needles:Array,$haystack:Array):Boolean
{
    for each(var item:Object in $needles) {
        if($haystack.indexOf(item) == -1) return false;
    }
    return true;
}


You may also want to consider putting your objects (if they are objects) in an object or dictionary. That way you avoid the horrible scaling behavior from doing a comparison over all of the components.


The most optimal solution would be to hash the items from the superset so that we can look them up in constant time. To do this we can use a Dictionary.

Then, we interate through the items in the subset to determine if they exist in the superset. Notice that we record the count, this ensures that we will also respect duplicate items.

The worst case complexity of this solution is O(M+N). However a failure case can complete with minimum O(M+1) since we break out of the second loop.

function compare(subSet:Array, superSet:Array):Boolean {

    // Assume true unless proven otherwise
    var retVal:Boolean = true;

    var map:Dictionary = new Dictionary();

    // Place all items from superSet into the dictionary map for constant time
    // lookup, also maintain a count for each item to record duplicates.
    var item:*;
    for each(item in superSet) {
        if(!map[item])
            map[item] = 0;

        // Increment the count for this item in the dictionary
        map[item]++;
    }

    for each(item in subSet) {
        if(map[item])
            // Item exists, decrement count.
            map[item]--;
        else {
            retVal = false;
            break;
        }
    }

    return retVal;
}


Have a look at Array.every() and Array.indexOf. However, even better perhaps might be to read a simple primer on how Arrays work in ActionScript.


FlexFiend pointed you in the right direction. Dictionaries are great for such problems, as they have O(1) for key lookup/insertion.

function contains(superSet:Array, subSet:Array):Boolean {
    var element:*,
        map:Dictionary = new Dictionary();
    for each (element in superSet) 
        map[element] = true;
    for each (element in subSet)
        if (map[element] != true) return false;
    return true;
}

This runs in O(M+N), which is as good as it gets on unsorted arrays.

0

精彩评论

暂无评论...
验证码 换一张
取 消