Sort by

recency

|

578 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

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

    Java using frequency map:

    public static int lonelyinteger(List<Integer> a) {
            if (a.size() == 1) return a.get(0);
            
            Map<Integer, Integer> map = new HashMap<>();
            
            for (int i : a) {
                map.put(i, map.getOrDefault(i, 0) + 1);
            }
    
            int result = 0;
            
            for (Map.Entry<Integer,Integer> e : map.entrySet()) {
                if (e.getValue() == 1) result = e.getKey();
            }
            
            return result;
        }