We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This problem is most optimally solved using bit manipulation.
The XOR logical function returns 1 when both inputs are diferent and 0 when they're the same, so 3 ^ 3 = 0 because in 11 xor 11 all digits are the same and they cancel out, this way we'll end up with the following: 0 ^ {lonelyInteger} which is the lonelyInteger itself:
Java code:
publicstaticintlonelyinteger(List<Integer>a){// Write your code hereintres=0;for(Integeri:a){res^=i;}returnres;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lonely Integer
You are viewing a single comment's thread. Return to all comments →
This problem is most optimally solved using bit manipulation. The XOR logical function returns 1 when both inputs are diferent and 0 when they're the same, so 3 ^ 3 = 0 because in 11 xor 11 all digits are the same and they cancel out, this way we'll end up with the following: 0 ^ {lonelyInteger} which is the lonelyInteger itself: Java code: