Skip to content

Instantly share code, notes, and snippets.

@raineorshine
Created June 29, 2016 18:23
Show Gist options
  • Save raineorshine/e080d25453a4c075cfcf89f72f8357cd to your computer and use it in GitHub Desktop.
Save raineorshine/e080d25453a4c075cfcf89f72f8357cd to your computer and use it in GitHub Desktop.
A Solidity library to remove elements from an array.
library ArrayUtil {
/** Finds the index of a given value in an array. */
function IndexOf(uint[] values, uint value) returns(uint) {
uint i = 0;
while (values[i] != value) {
i++;
}
return i;
}
/** Removes the given value in an array. */
function RemoveByValue(uint[] values, uint value) {
uint i = IndexOf(value);
RemoveByIndex(i);
}
/** Removes the value at the given index in an array. */
function RemoveByIndex(uint[] values, uint i) {
while (i<values.length-1) {
values[i] = values[i+1];
i++;
}
values.length--;
}
}
@Piliponful
Copy link

And you don't pass values(array on which to execute operation) anywhere.

@jigar23
Copy link

jigar23 commented Jun 26, 2018

You're not receiving the uint[] as a storage, I believe it will create a copy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment