Submission 51b878a8...

ChallengeRemove duplicate elements
Submitterricmoo.firefly.eth
Submitted at
Gas used2860583
/**
 * 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/
 *
 *  Author: Richard Moore <[email protected]>
 *  Date:   May 24, 2018
 */

pragma solidity 0.4.24;

contract Unique {
    function uniquify(uint[] input) public pure returns(uint[] ret) {
        uint256 bloomFilter = 0;
        uint256 ptr = 0;

        uint256 length = input.length;
        for (uint256 i = 0; i < length; i++) {
            uint256 v = input[i];
            uint256 filter = (uint256(1) << uint8(v * 211));

            uint256 notUnique = (bloomFilter & filter);
            if (notUnique != 0) {
                notUnique = 0;
                for (uint256 j = 0; j < i; j++) {
                    if (input[j] == v) {
                        notUnique = 1;
                        break;
                    }
                }
            }

            if (notUnique == 0) {
                input[ptr++] = v;
                bloomFilter |= filter;
            }
        }

        ret = new uint[](ptr);
        for(i = 0; i < ret.length; i++) {
            ret[i] = input[i];
        }

        return ret;
    }
}