Sort by

recency

|

580 Discussions

|

  • + 0 comments

    I was too bored to use count(): def lonelyinteger(a): s = list(set(a)) for number in s: try: a.remove(number) a.remove(number) except ValueError as V: return(number)

  • + 0 comments

    XOR

    def lonely_integer(arr):
        result = 0
        for num in arr:
            result ^= num
        return result
    
  • + 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

    My Java solution with o(n) time complexity and constant space:

    public static int lonelyinteger(List<Integer> a) {
            if(a.size() == 1) return a.get(0); //only element available
            
            //use XOR to compare elements
            int res = 0;
            for(int i = 0; i < a.size(); i++) res = a.get(i) ^ res;
            return res;
        }
    
  • + 0 comments
    def lonelyinteger(a):
        for i in a:
            if(a.count(i)==1):
                return i