Submission 68557fb4...

ChallengeRemove duplicate elements
Submitter0x888888417655103...
Submitted at2018-31-30
Gas used275706
pragma solidity 0.4.24;

contract Unique {

    function uniquify(
        uint[] input
    ) public pure returns(uint[]) {
        assembly {
            let inLen := calldataload(0x24)

            if lt(inLen, 2) {
                mstore(sub(input, 0x20), 0x20)
                return(sub(input, 0x20), add(mul(0x20, inLen), 0x40))
            }

            function uniquifyBloom(inBegin, inEnd) {
                let filter := 0
                let outPtr := inBegin
                for { let idx := inBegin }
                    lt(idx, inEnd)
                    { idx := add(idx, 0x20) } {
                    let v := mload(idx)
                    let y := and(
                        exp(0x02, mod(v, 0xf1)),
                        exp(0x02, mod(v, 0x97)))
                    if iszero(and(filter, y)) {
                        filter := or(filter, y)
                        mstore(outPtr, v)
                        outPtr := add(outPtr, 0x20)
                    }
                }
                mstore(sub(inBegin, 0x20), div(sub(outPtr, inBegin), 0x20))
            }

            uniquifyBloom(
                add(input, 0x20),
                add(add(input, 0x20), mul(0x20, inLen)))
            mstore(sub(input, 0x20), 0x20)
            return(sub(input, 0x20), add(mul(0x20, mload(input)), 0x40))
        }
    }
}