Skip to content

Instantly share code, notes, and snippets.

@raineorshine
Created June 29, 2016 18:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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--;
}
}
@danalvi
Copy link

danalvi commented Jan 27, 2017

your function is going to go in an infinite loop if the element to be deleted is not present

@Piliponful
Copy link

Piliponful commented Sep 20, 2017

You should make a new variable length. It won't compile at least in the latest versions of solidity.

@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