Skip to main content
Accidentally put != instead of ==, and moved the break statement.
Source Link

I've probably made some minor mistakes here but you get the jist of it. Use keccak256(abi.encodePacked("ABC")) to be able to compare strings. Then loop through one element of list2 at a time and compare that to all the elements in list1, when it sees an element that is the same, it adds that to the new list, breaks out of the inner loop and goes to the next element in list2 and compares that to all elements in list1.. and so on.


    // Output 1: items in listTwo that do not exist in listOne   
    function compare() public view returns(string[] memory) {
        // write to memory to save gas when looping through
        string[5] memory list1 = listOne; 
        string[5] memory list2 = listTwo;

        string[] memory l2NotInl1;l2Inl1;
        uint nonce;
        for(uint i; i < 5; i++){
            for(uint j; j < 5; j++){
                if(keccak256(abi.encodePacked(list2[i])) !=== keccak256(abi.encodePacked(list1[j]))){
                    l2NotInl1[nonce]l2Inl1[nonce] = list2[i];
                    nonce++;
                } else {
                    break;break
                }
            } 
        }
        return l2NotInl1;l2Inl1;
    }

You would need to construct something like this for each output that you need. Depending on what chain you run this, the gas cost of running keccak256(abi.encodePacked()) on every string might get costly. I would consider doing this off chain. Or first converting the strings to (or from the start using) a list of bytes, which you can compare. However, converting them back might return unexpected results, it all depends on your use case, which I don't know. But This should hopefully give you some ideas at least!

I've probably made some minor mistakes here but you get the jist of it. Use keccak256(abi.encodePacked("ABC")) to be able to compare strings. Then loop through one element of list2 at a time and compare that to all the elements in list1, when it sees an element that is the same, it breaks out of the inner loop and goes to the next element in list2 and compares that to all elements in list1.. and so on.


    // Output 1: items in listTwo that do not exist in listOne   
    function compare() public view returns(string[] memory) {
        // write to memory to save gas when looping through
        string[5] memory list1 = listOne; 
        string[5] memory list2 = listTwo;

        string[] memory l2NotInl1;
        uint nonce;
        for(uint i; i < 5; i++){
            for(uint j; j < 5; j++){
                if(keccak256(abi.encodePacked(list2[i])) != keccak256(abi.encodePacked(list1[j]))){
                    l2NotInl1[nonce] = list2[i];
                    nonce++;
                } else {
                    break;
                }
            } 
        }
        return l2NotInl1;
    }

You would need to construct something like this for each output that you need. Depending on what chain you run this, the gas cost of running keccak256(abi.encodePacked()) on every string might get costly. I would consider doing this off chain. Or first converting the strings to (or from the start using) a list of bytes, which you can compare. However, converting them back might return unexpected results, it all depends on your use case, which I don't know. But This should hopefully give you some ideas at least!

I've probably made some minor mistakes here but you get the jist of it. Use keccak256(abi.encodePacked("ABC")) to be able to compare strings. Then loop through one element of list2 at a time and compare that to all the elements in list1, when it sees an element that is the same, it adds that to the new list, breaks out of the inner loop and goes to the next element in list2 and compares that to all elements in list1.. and so on.


    //Output: items in listTwo that exist in listOne
    function compare() public view returns(string[] memory) {
        // write to memory to save gas when looping through
        string[5] memory list1 = listOne; 
        string[5] memory list2 = listTwo;

        string[] memory l2Inl1;
        uint nonce;
        for(uint i; i < 5; i++){
            for(uint j; j < 5; j++){
                if(keccak256(abi.encodePacked(list2[i])) == keccak256(abi.encodePacked(list1[j]))){
                    l2Inl1[nonce] = list2[i];
                    nonce++;
                    break
                }
            } 
        }
        return l2Inl1;
    }

You would need to construct something like this for each output that you need. Depending on what chain you run this, the gas cost of running keccak256(abi.encodePacked()) on every string might get costly. I would consider doing this off chain. Or first converting the strings to (or from the start using) a list of bytes, which you can compare. However, converting them back might return unexpected results, it all depends on your use case, which I don't know. But This should hopefully give you some ideas at least!

Source Link

I've probably made some minor mistakes here but you get the jist of it. Use keccak256(abi.encodePacked("ABC")) to be able to compare strings. Then loop through one element of list2 at a time and compare that to all the elements in list1, when it sees an element that is the same, it breaks out of the inner loop and goes to the next element in list2 and compares that to all elements in list1.. and so on.


    // Output 1: items in listTwo that do not exist in listOne   
    function compare() public view returns(string[] memory) {
        // write to memory to save gas when looping through
        string[5] memory list1 = listOne; 
        string[5] memory list2 = listTwo;

        string[] memory l2NotInl1;
        uint nonce;
        for(uint i; i < 5; i++){
            for(uint j; j < 5; j++){
                if(keccak256(abi.encodePacked(list2[i])) != keccak256(abi.encodePacked(list1[j]))){
                    l2NotInl1[nonce] = list2[i];
                    nonce++;
                } else {
                    break;
                }
            } 
        }
        return l2NotInl1;
    }

You would need to construct something like this for each output that you need. Depending on what chain you run this, the gas cost of running keccak256(abi.encodePacked()) on every string might get costly. I would consider doing this off chain. Or first converting the strings to (or from the start using) a list of bytes, which you can compare. However, converting them back might return unexpected results, it all depends on your use case, which I don't know. But This should hopefully give you some ideas at least!