Skip to content

Instantly share code, notes, and snippets.

@seresistvanandras
Last active June 14, 2018 07:38
Show Gist options
  • Save seresistvanandras/e46fede741530c7fd88382704cf5a326 to your computer and use it in GitHub Desktop.
Save seresistvanandras/e46fede741530c7fd88382704cf5a326 to your computer and use it in GitHub Desktop.
function uniquify(uint[] input) public pure returns(uint[] ret) {
      if(input.length < 2) {
          return input;
      }

      uint[256][2] memory hashTable;
      uint[] memory index = new uint[](input.length);
      uint counter;
      bool nullValue;

      for(uint i = 0; i < input.length; i++) {
          uint hash = input[i] &255;

          if(hashTable[0][hash]==0 && input[i] != 0) {
              hashTable[0][hash] = input[i];
              index[counter] = input[i];
              counter++;
          }
          else if(hashTable[0][hash]!=0 && input[i]!=hashTable[0][hash]) {
              hashTable[1][hash] = input[i];
              index[counter] = input[i];
              counter++;
          }
          else if(!nullValue && input[i] == 0){
              nullValue = true;
              hashTable[0][hash] = 0;
              index[counter] = 0;
              counter++;
          }
      }

      ret = new uint[](counter);
      for(i=0; i < counter; i++) {
          ret[i] = index[i];
      }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment