Skip to content

Instantly share code, notes, and snippets.

@V0L0DYMYR
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save V0L0DYMYR/7675c5455ea9f952ce4f to your computer and use it in GitHub Desktop.

Select an option

Save V0L0DYMYR/7675c5455ea9f952ce4f to your computer and use it in GitHub Desktop.
Lonely number
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