Lonely Integer

Sort by

recency

|

232 Discussions

|

  • + 0 comments

    yo guys this is simpler and more effecient.....

    from collections import Counter as c

    at #write your code replace it with res=c(a) for i in a: if res[i]==1: return i

  • + 0 comments

    The solution to the problem is by summing xor values;

    return std::accumulate(a.begin(), a.end(), 0, std::bit_xor<int>{});
    
  • + 0 comments

    ** int lonelyinteger(vector a) { map freq; for(int i=0;i for(auto i:freq) { if(i.second == 1) { return (i.first); }

    }
    return -1;
    

    } **

  • + 0 comments

    Save time and space I believe

    int result = 0; Map count = new HashMap<>();

    for(int num : a) { if(count.containsKey(num)) { count.remove(num); } else { count.put(num, 0); } } for(Map.Entry entry : count.entrySet()) { result = entry.getKey(); }

        return result;
    
  • + 0 comments

    // Using map --> create map // key--> item, value --> occurences of item in input array (a) // iterate through map --> return key whose value is 1

    let numMap = new Map()
    
    a.forEach(item=> {
        if(numMap.has(item)){
            numMap.set(item,numMap.get(item)+1)
        } else {
            numMap.set(item,1)
        }
    })
    
    for (let [key,value] of numMap) {
        if(value===1) { return key}
    }