Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitscript authored Oct 2, 2022
1 parent 42a4a9e commit 41bf3db
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 20Day-SecondLargestNumber.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <0.9.0;

contract Day2 {
//Create a function secondMax(array, length of array) . This secondMax() will take two arguments - a dynamic int type array and length of the array.
function secondMax(int256[] memory arr, uint256 size) public pure returns(int256) {
for (uint256 i = 0; i < size - 1; i++) {
for (uint256 j = i + 1; j < size; j++) {
if (arr[i] < arr[j]) {
int256 temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

return arr[1];
}
}

//The secondMax() will find the second largest element in an array. For Example - If array =[40, 12, 31, 6] then secondMax() will return 31. If array =[9, 122, 37, 54] then secondMax() will return 54.

0 comments on commit 41bf3db

Please sign in to comment.