Lonely Integer

Sort by

recency

|

457 Discussions

|

  • + 0 comments

    Python solution using dictionary instead of Counter method

    def lonelyinteger(a):
        count = {}
        for i in a:
            if i in count:
                count[i] += 1
            else:
                count[i] = 1
        
        out = [ele for ele in count.keys() if count[ele] == 1][0]
        return out
    
  • + 0 comments

    public static int lonelyinteger(List a) { // Write your code here int unique = 0;

        for(int i : a){
            unique = unique ^ i;
        }
        return unique;
    }
    
  • + 0 comments
    from collections import Counter
    def lonelyinteger(a):
        # Write your code here
        data = Counter(a)
        unique_elements = [key for key, value in data.items() if value == 1]
        print(unique_elements[0])
        return unique_elements[0]
        
    
  • + 0 comments
    from collections import Counter
    def lonelyinteger(a):
        # Write your code here
        data = Counter(a)
        unique_elements = [key for key, value in data.items() if value == 1]
        print(unique_elements[0])
        return unique_elements[0]
        
    
  • + 0 comments

    Java

    public static int lonelyinteger(List<Integer> a) {
        // Write your code here
            Collections.sort(a);
            for(int i = 0; i < a.size(); i += 2){
                if( i == a.size() -1 || a.get(i) != a.get(i + 1))
                    return a.get(i);
            }
            return 0;
        }