• + 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;
        }