Last active
August 29, 2015 14:16
-
-
Save V0L0DYMYR/7675c5455ea9f952ce4f to your computer and use it in GitHub Desktop.
Lonely number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class LonelyNumber { | |
| public static void main(String[] args) { | |
| int[] arr = {1, 2, 4, 2, 3, 7, 2, 1, 3, 4, 1, 3, 4}; | |
| int[] bits = new int[32]; // will acomulate bits here | |
| for (int num : arr) { | |
| for (int i = 0; i < bits.length; i++) { // go through all bits in a number | |
| int oneBitNum = 1 << i; | |
| if ((num & oneBitNum) > 0) { // check if bit on i-th position equals 1 | |
| bits[i] += 1; | |
| } | |
| } | |
| } | |
| int result = 0; | |
| for (int i = 0; i < bits.length; i++) { | |
| if (bits[i] % 3 == 1) { | |
| result |= 1 << i; // set 1 on i-th posotion | |
| } | |
| } | |
| System.out.println(result); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment