Submission dde4d7a4...

ChallengeRemove duplicate elements
Submitter0xff3c2241cd767b7...
Submitted at2018-14-26
Gas used767809
/**
 * 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 {

    // This implementation:    966628

    /**
     * @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) {
      uint256 lenmod = input.length+92; // Instead of being relative, this could just be tweaked...
      // Oh, we can't have mappings in memory? Then we're going to make our own rubbish one! :D
      uint256[] memory processedvalue = new uint256[](lenmod+1);
      bool[] memory processedbool = new bool[](lenmod+1);

      uint slot;
      uint nuniques;
      for (uint i =0; i<input.length; i+=1){
        slot = input[i] % (lenmod);
        while (processedbool[slot] == true && processedvalue[slot] != input[i]){
          slot +=1;
          if (slot > lenmod){
            slot=0;
          }
        }
        if (processedbool[slot]==false){
          // Then we've not seen it before
          processedbool[slot] = true;
          processedvalue[slot] = input[i];
          input[nuniques] = input[i];
          nuniques += 1;
        }
      }
      // create output array
      ret = new uint[](nuniques);
      for (i = 0; i<ret.length; i+=1){
        ret[i] = input[i];
      }
      return ret;
    }

}