Submission eaefa737...

ChallengeRemove duplicate elements
Submittermichaelsproul.eth
Submitted at2018-35-01
Gas used5100439
/**
 * 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) {
        uint n = input.length;

        uint i;
        uint num_uniq;

        for (i = 0; i < n; i++) {
            uint val = input[i];

            bool is_duplicate = false;

            for (uint j = 0; j < num_uniq; j++) {
                if (val == input[j]) {
                    is_duplicate = true;
                    break;
                }
            }

            if (!is_duplicate) {
                input[num_uniq] = val;
                num_uniq++;
            }
        }

        ret = new uint[](num_uniq);

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