let's say I have an group contract where users can join and leave groups. When a user joins a group, his address will be added to members[groupId]. When he lefts, the members[groupId] will be set to 0x0. Let's say members[0] is 2,000 values long. The problem is, that not all of these 2,000 members are active/in the group as some addresses in that array are 0x0 addresses.

Let's say I have now a website where I want to get all active members of group 0. What are now the best ways to get the active users? A 2,000 times loop in javascript? Or are there maybe more efficient ways to solve it?


You can maintain a mapping like mapping (bool => Member[]) isActiveMembers with help of which you will be to reduce time complexity of getActiveMembers function from O(N) to O(1).

All the active members will have the key true and when active members leave the group you can remove the respective member from the mapping isActiveMembers with key true and insert the same member in the mapping isActiveMembers with key false.

Let me know if any further clarification is required.

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.