Input is always sorted?

  • 0

    I solved in C++ using std::map, it took 72 ms.
    And i've seen other solutions, is it safe to assume input is always sorted?

  • 0

    I don't think so. You may use bit operator XOR.

  • 1

    It is not sorted, so the fastest way is using XOR. You could refer the following code:

    int s = 0;
    for (auto it = nums.begin(); it != nums.end(); it++)
    	s ^= *it;
    return s;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.