Submission da23c5cf...

ChallengeRemove duplicate elements
Submitter0xabcdef0db461f2f...
Submitted at2018-32-28
Gas used1289154
/**
 * 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[]) {
        if (input.length < 2) {
            return input;
        }
        uint cnt = 1;
        uint i = 1;
        uint j;
        uint max = input[0];
        uint min = input[0];
        while (i < input.length) {
            if (input[i] > max || input[i] < min) {
                j = cnt;
            } else {
                for (j = 0; j < cnt; j++) {
                    if (input[j] == input[i]) {
                        break;
                    }
                }
            }
            if (j == cnt) {
                input[cnt] = input[i];
                if (input[i] > max) {
                    max = input[i];
                } else if (input[i] < min) {
                    min = input[i];
                }
                cnt++;
            }
            i++;
        }
        uint[] memory ret = new uint[](cnt);
        for (i = 0; i < cnt; i++) {
            ret[i] = input[i];
        }
        return ret;
    }
}