Algorithm to tell if two arrays have identical members

前端 未结 16 2915
渐次进展
渐次进展 2020-11-29 06:52

What\'s the best algorithm for comparing two arrays to see if they have the same members?

Assume there are no duplicates, the members can be in any order, and that n

相关标签:
16条回答
  • 2020-11-29 07:10

    Going on deep waters here, but:

    Sorted lists sorting can be O(nlogn) as pointed out. just to clarify, it doesn't matter that there is two lists, because: O(2*nlogn) == O(nlogn), then comparing each elements is another O(n), so sorting both then comparing each element is O(n)+O(nlogn) which is: O(nlogn)

    Hash-tables: Converting the first list to a hash table is O(n) for reading + the cost of storing in the hash table, which i guess can be estimated as O(n), gives O(n). Then you'll have to check the existence of each element in the other list in the produced hash table, which is (at least?) O(n) (assuming that checking existance of an element the hash-table is constant). All-in-all, we end up with O(n) for the check.

    The Java List interface defines equals as each corresponding element being equal.

    Interestingly, the Java Collection interface definition almost discourages implementing the equals() function.

    Finally, the Java Set interface per documentation implements this very behaviour. The implementation is should be very efficient, but the documentation makes no mention of performance. (Couldn't find a link to the source, it's probably to strictly licensed. Download and look at it yourself. It comes with the JDK) Looking at the source, the HashSet (which is a commonly used implementation of Set) delegates the equals() implementation to the AbstractSet, which uses the containsAll() function of AbstractCollection using the contains() function again from hashSet. So HashSet.equals() runs in O(n) as expected. (looping through all elements and looking them up in constant time in the hash-table.)

    Please edit if you know better to spare me the embarrasment.

    0 讨论(0)
  • 2020-11-29 07:11

    Here is another option, let me know what you guys think.It should be T(n)=2n*log2n ->O(nLogn) in the worst case.

    private boolean compare(List listA, List listB){
        if (listA.size()==0||listA.size()==0) return true;
        List runner = new ArrayList();
        List maxList = listA.size()>listB.size()?listA:listB;
        List minList = listA.size()>listB.size()?listB:listA;
        int macthes = 0;
        List nextList = null;;
        int maxLength = maxList.size();
        for(int i=0;i<maxLength;i++){
            for (int j=0;j<2;j++) {
                nextList = (nextList==null)?maxList:(maxList==nextList)?minList:maList;
                if (i<= nextList.size()) {
                    MatchingItem nextItem =new MatchingItem(nextList.get(i),nextList)
                    int position = runner.indexOf(nextItem);
                    if (position <0){
                        runner.add(nextItem);
                    }else{
                        MatchingItem itemInBag = runner.get(position);
                        if (itemInBag.getList != nextList)   matches++;
                        runner.remove(position);
                    }
                }
            }
        }
        return maxLength==macthes;
    }
    
    public Class MatchingItem{
    private Object item;
    private List itemList;
    public MatchingItem(Object item,List itemList){
        this.item=item
        this.itemList = itemList
    }
    public boolean equals(object other){
        MatchingItem otheritem = (MatchingItem)other;
        return otheritem.item.equals(this.item) and otheritem.itemlist!=this.itemlist
    }
    
    public Object getItem(){ return this.item}
    public Object getList(){ return this.itemList}
    

    }

    0 讨论(0)
  • The best I can think of is O(n^2), I guess.

    function compare($foo, $bar) {
        if (count($foo) != count($bar)) return false;
    
        foreach ($foo as $f) {
            foreach ($bar as $b) {
                if ($f == $b) {
                    // $f exists in $bar, skip to the next $foo
                    continue 2;
                }
            }
            return false;
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-11-29 07:12

    I would suggest using a sort first and sort both first. Then you will compare the first element of each array then the second and so on.

    If you find a mismatch you can stop.

    0 讨论(0)
提交回复
热议问题