1ms JAVA solution

  • 54

    public class Solution {

    public int singleNumber(int[] nums) {
        int result = 0;
        for(int i : nums) {
            result ^= i;
        return result;


  • 2

    I wonder why the same algorithm would cost 20ms using c++.

  • 0

    awesome sign of programming heads of

  • 0

    That's quite interesting. Though my C++ skills are not that great and I don't know the reason, I definitely want to try to do this in C++ and see if I have the same problem.
    Also, sometimes the Leetcode runtimes change if you run your code multiple times. You may want to try to submit this again and see if it still takes 20ms.

  • 0

    How can this be done in python, this code malfunctions for [1,0,1] test case in python.

Log in to reply

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