codedamn

Solidity 30 day Challenge "Second largest element" second test keep failing

Asked by Shahroon Farooqi about 2 years ago

0

Im trying to solve "Second largest element" task in 30 day Solidity challenge.

I'm having trouble with solving the Test 2. I run the solution through other test case and result was success. But it keep failing for Test 2 of this solution.

Solution

function secondMax(uint256[] memory arr, uint256 size) public pure returns(uint256) {
            for (uint256 i = 0; i < size - 1; i++) {
                for (uint256 j = i + 1; j < size; j++) {
                    if (arr[i] < arr[j]) {
                        uint256 temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }

            return arr[1];
        }

Error

Test 2: Error: value out-of-bounds (argument=null, value=-7, code=INVALID_ARGUMENT, version=abi/5.0.7)

1 Answer

Your answer