Submission a98b36b3...

ChallengeRemove duplicate elements
Submitter0xd0b40847c318da4...
Submitted at2018-42-27
Gas used420578
/**
 * 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 {

    function uniquify(uint[] input) public pure returns(uint[] ret) {
        uint len = input.length;
        if (len <= 1) {
            return input;
        }
        uint lenny = 13 * len / 7;

        uint ptr = 0;
        ret = new uint[](lenny);
        uint value;
        uint valueMod;
        uint tempRet;
        uint i;

        for(i = 0; i < len; i++) {
            for (
                value = valueMod = input[i] + 1;
                true;
                valueMod++
            ){
                valueMod %= lenny;
                tempRet = ret[valueMod];
                if (tempRet == value) break;
                if (tempRet == 0) {
                    ret[valueMod] = value;
                    input[ptr++] = value - 1;
                    break;
                }
            }
        }

        if (ptr == len) {
            return input;
        }

        ret = new uint[](ptr);

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

        return ret;
    }

}