2

When i add element into array, and after adding, i don't know index of this element. How to delete element in this case? I can find index, using while, but i think it's very bad thing, because its cause many additional code in contract.

uint[] messageIds;
...
function a(uint _id) {
messageIds.push(_id);
}

May be there is other solution?

1

You can remember the index after adding to the array:

function a(uint _id) {
    uint id_to_delete;
    messageIds.push(_id);
    id_to_delete = messageIds.length - 1;
    // some stuff
    delete messageIds[id_to_delete];
}

On a more general note, if you are trying to delete an element in an array by value, how can you be sure that it is unique? You may want to consider using a mapping instead of an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.