Lonely Integer

Sort by

recency

|

445 Discussions

|

  • + 0 comments

    def lonelyinteger(a): # Write your code here for i in a: if a.count(i)==1: result=i return i

  • + 1 comment

    Hi

    I have got one invalid test case here where the array is 0 0 1 2 1 , so the unique element count should be 3 but its coming 2 as expected output ,causing my tests to fail , anybody faced this or correct me?

  • + 0 comments
    def lonelyinteger(a):
        dict_occ = {}
        for num in a:
            dict_occ[num] = 0
            for n in a:
                if num == n:
                    dict_occ[num] += 1
        for key, val in dict_occ.items():
            if val == 1:
                return key
    
  • + 0 comments

    My solution in Rust, it takes advantage of the XOR properties. they key is that every has only one duplicate eccept one (correct me if I'm wrong) and XOR is commutative and associative:

    fn lonelyinteger(a: &[i32]) -> i32 {
        a.iter().fold(0, |acc, x| *x ^ acc)
    }
    
  • + 0 comments

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