I am trying to implement the following exercise "Santa's list": Build a system for tracking who is on their nice list and who is on their naughty list.

Furthermore, I do not want duplicates in my list (even though I will input common names).

Just focusing on adding people to the list I wrote the following:

pragma solidity 0.8.1;

contract SantasList {

mapping (string => uint) naughtyList;
mapping (string => uint) niceList;
mapping (string => bool) addedOrNot;
string[] addedPeople;

function addToList(string memory _name, bool _isnice) public returns(string memory _alreadyadded){
    if (addedOrNot[_name] == false){
        if (_isnice == true){
            niceList[_name] = addedPeople.length+1;
        } else {
            naughtyList[_name] = addedPeople.length+1;
    return ("added");
    } else {
    return ("alreadyAdded");

Would you have any other better way to implement this? what can I improve? Please note I am trying to avoid iteration to be efficient on Gas.

Your Answer

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

Browse other questions tagged or ask your own question.