Submission 644669bc...

ChallengeRemove duplicate elements
Submitter0x53b21be84eece2e...
Submitted at2018-19-01
Gas used309063
/**
 * 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.
     */

    // 778298
    // 652237
    function uniquify(uint[] input) public pure returns(uint[] ret) {
        uint i = 0;
        uint j = 0;
        uint len = input.length;
        // uint mod = len;
        // uint[] memory hashmap = new uint[](len);
        uint buckets;
        ret = input;
        for (; i < len; i++) {
            uint val = input[i];
            // uint mapval = val % len;
            uint mapbit = 2 ** val;
            if ((buckets & mapbit) == 0) {
                buckets |= mapbit;
            } else {
                j = i;
                for (i++; i < len; i++) {
                    val = input[i];
                    // mapval = val % len;
                    mapbit = 2 ** val;
                    if ((buckets & mapbit) == 0) {
                        buckets |= mapbit;
                        input[j++] = val;
                    }
                }
                ret = new uint[](j);
                for (i = 0; i < j; i++) {
                    ret[i] = input[i];
                }
                i = len;
            }
        }
    }
}
// 1000 793341
// 2000 883471
// 3000 1000949