Created
March 12, 2019 08:24
-
-
Save islishude/e0e241cb24609be1d83e62d086010654 to your computer and use it in GitHub Desktop.
Solidity remove element from array
This file contains hidden or 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.2; | |
contract Array { | |
uint256[] internal numbers; | |
constructor() public { | |
numbers.push(1); | |
numbers.push(2); | |
numbers.push(3); | |
} | |
function add(uint256 i) public { | |
numbers.push(i); | |
} | |
function removeByUnorder(uint256 idx) public { | |
if (idx >= numbers.length) { | |
return; | |
} | |
if (idx == numbers.length-1) { | |
numbers.length--; | |
return; | |
} | |
numbers[idx] = numbers[numbers.length-1]; | |
numbers.length--; | |
} | |
function removeByOrder(uint256 idx) public { | |
if (idx >= numbers.length) { | |
return; | |
} | |
for (uint i = idx; i<numbers.length-1; i++){ | |
numbers[i] = numbers[i+1]; | |
} | |
numbers.length--; | |
} | |
function getNumbers() public view returns (uint256[] memory){ | |
return numbers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment