Submission e3937f72...

ChallengeRemove duplicate elements
Submitter0x9e0ad99de20a444...
Submitted at2018-45-26
Gas used639333
/**
 * This file is part of the 1st Solidity Gas Golfing Contest.
 *
 * This work is licensed under Creative Commons Attribution ShareAlike 3.0.
 * https://creativecommons.org/licenses/by-sa/3.0/
 */

pragma solidity 0.4.24;

contract Unique {
 /**
 * @dev Removes all but the first occurrence of each element from a list of
 * integers, preserving the order of original elements, and returns the list.
 *
 * The input list may be of any length.
 *
 * @param input The list of integers to uniquify.
 * @return The input list, with any duplicate elements removed.
 */
    function uniquify(uint[] input) public pure returns(uint[] ret){
        //v2.0
        uint counter;
        bool flagZero;
        uint inputLen = input.length;
        uint[256][2] memory table;
        uint[] memory index = new uint[](inputLen);

        if(inputLen < 2)
            return input;

        for(uint i = 0; i < inputLen; i++){
            uint element = input[i];
            uint indexTable = element & 255;

            if(table[0][indexTable]==0 && element != 0){
                table[0][indexTable] = element;
                index[counter] = element;
                counter++;
                continue;
            }
            if(table[0][indexTable]!=0 && element!=table[0][indexTable]){
                table[1][indexTable] = element;
                index[counter] = element;
                counter++;
                continue;
            }
            if(!flagZero && element == 0){
                flagZero = true;
                table[0][indexTable] = 0;
                index[counter] = 0;
                counter++;
            }
        }

        ret = new uint[](counter);
        for(i = 0; i < counter; i++)
            ret[i] = index[i];
    }
}