Sort by

recency

|

559 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the vidéo explanation here : https://youtu.be/E4PVCUOdeXM

        int result = 0;
        for(int x: a) result ^= x;
        return result;
    }
    
  • + 0 comments

    Two solutions in kotlin one using a set and the other using xor

    XOR:

    fun lonelyinteger(a: Array<Int>): Int {
        var result = 0
        for (n in a) {
            result = result xor n
        }
        return result
    }
    

    Set:

    fun lonelyinteger(a: Array<Int>): Int {
        val bag = mutableSetOf<Int>()
        for (n in a) {
            if (bag.contains(n)) {
                bag.remove(n)
            } else {
                bag.add(n)
            }
            
        }
        return bag.toList()[0]
    }
    
  • + 0 comments

    python 3

    def lonelyinteger(a): # Write your code here return [x for x in a if a.count(x) == 1][0]

  • + 0 comments

    An one-liner in Python:

    return 2*sum(set(a))-sum(a)

  • + 0 comments
    Java 8
    public static int lonelyinteger(List<Integer> a) {
        // Write your code here
        int n=0;
        for(int i=0;i<a.size();i++)
        {
            if(a.indexOf(a.get(i))==a.lastIndexOf(a.get(i)))
            {
                n=a.get(i);
            }
        }
         return n;
        }
    
    }
    

    `