Sort by

recency

|

579 Discussions

|

  • + 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
    
  • + 0 comments

    This one is so easy! Here is my Python solution!

    def lonelyinteger(a):
        for num in a:
            if a.count(num) == 1:
                return num