Here is my code :

pragma solidity ^0.4.14;

contract AdminSetUp {   // contract for adding new admin
address private creator = msg.sender;
address[] public adminlist;
uint public count;

modifier onlyCreator {  // only creator of contract is allowed to add admin
    if (msg.sender == creator)

function addMember(address _newadmin) public onlyCreator returns (uint) {
    count = adminlist.length;
    return count;


this code allows to add admin those are already there. How to avoid this?

2 Answers 2


Iterate through the array and make sure that the _newadmin != match any existing array entries.


Possibly convert to use a mapping(address -> bool) isAdmin instead. Which would prevent having to iterate through the entire array. As a simple example below:

pragma solidity ^0.4.18;

import "github.com/OpenZeppelin/zeppelin-solidity/contracts/ownership/Ownable.sol";

contract AdminSetUp is Ownable{
  struct Admin {
        uint adminData;
        bool isAdmin;
  mapping (address => Admin) public admins;

  function isAdmin(address _address) public view returns(bool _isAdmin) {
    return admins[_address].isAdmin;

  function newAdmin(address _address, uint _adminData) public onlyOwner returns(bool success) {
    // Make sure admin doesn't already exist and validate _address
    require(!isAdmin(_address) && _address != address(0));
    admins[_address].adminData = _adminData;
    admins[_address].isAdmin = true;
    return true;

  function removeAdmin(address _address) public onlyOwner returns(bool success) {
    // Make sure admin exists and validate _address
    require(isAdmin(_address) && _address != address(0));
    admins[_address].isAdmin = false;
    return true;

  function updateAdminData(address _address, uint _adminData) public onlyOwner returns(bool success) {
    admins[_address].adminData = _adminData;
    return true;

I've done a slightly different approach:

Admin Struct, with an Id and the address (you can add more things if you want)

struct Admin {
        uint256 adminId;
        address adminAddress;

Here I'm mapping the Admin Struct in an Unordered list.

    address[] private adminIndex;
    mapping(address => Admin) mapping_admin;

Function to check if the admin is already registered.

function adminExist(address newAdmin) public view returns (bool adminExist) {
        if (adminIndex.length == 0)
            return false;

        return (adminIndex[mapping_admin[newAdmin].adminId] == newAddress);

Function to register a admin with a validation.

Here, is where the magic happens, I'm matching the Array Index with the adminId.

    function addAdmin(address adminAddress) onlyCreator public returns (uint256) {
        require(adminAddress!= address(0));

        if (!adminExist(adminAddress)) {
            var newAdmin = Admin(adminIndex.length, adminAddress);

            mapping_admin[adminAddress] = newAdmin ;

            return newAdmin.adminId;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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