Created
October 1, 2020 16:06
-
-
Save Mayur1496/955566b948324480d9fc7bb957dd88bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.5; | |
library Search { | |
function indexOf(uint[] storage self, uint value) public view returns (uint) { | |
for (uint i = 0; i < self.length; i++) if (self[i] == value) return i; | |
return uint(-1); | |
} | |
} | |
contract Test { | |
uint[] data; | |
constructor() public { | |
data.push(1); | |
data.push(2); | |
data.push(3); | |
data.push(4); | |
data.push(5); | |
} | |
function isValuePresent() external view returns(uint){ | |
uint value = 4; | |
//search if value is present in the array using Library function | |
uint index = Search.indexOf(data, value); | |
return index; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment