Lonely Integer

Sort by

recency

|

893 Discussions

|

  • + 0 comments

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

  • + 0 comments
    def lonelyInteger (a):
    sortedA = sorted(a)  
    lonelyInteger = 0
    for i in range(0,len(sortedA)):
    	lonelyInteger ^= sortedA[i]
            
    return lonelyInteger
    
  • + 0 comments

    This is a simple solution in python, reduce applies a accumulative operation to all elements in the list into a single value Python:

    from functools import reduce
    from operator import ixor
    
    def lonelyinteger(a):
        return reduce(ixor, a)
    
  • + 0 comments

    Single line js solution:

    const lonelyinteger = (a) => (a.reduce((x, y) => x^=y));
    
  • + 0 comments

    golang solution using XOR:

    func lonelyinteger(a []int32) int32 {
        var unique int32 = 0
        
        for _, i := range a{
            unique ^= i
        }
        
        return unique
    
    }