Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stddef.h>
- #define COUNTS_LEN 128
- int find_odd (size_t length, const int array[length]) {
- // counts of positive numbers
- int pos_counts[COUNTS_LEN];
- // counts of negative numbers; indices are absolute values
- int neg_counts[COUNTS_LEN];
- // initialize counts arrays to -1
- for (int i = 0; i < COUNTS_LEN; i++)
- neg_counts[i] = pos_counts[i] = -1;
- // count up numbers
- for (int i = 0; i < length; i++) {
- int n = array[i];
- if (n < 0) {
- if (neg_counts[-n] < 0)
- neg_counts[-n] = 0;
- neg_counts[-n] += 1;
- } else {
- if (pos_counts[n] < 0)
- pos_counts[n] = 0;
- pos_counts[n] += 1;
- }
- }
- // check pos counts
- for (int i = 0; i < COUNTS_LEN; i++)
- if (pos_counts[i] >= 0 && pos_counts[i] % 2 != 0)
- return i;
- // check neg counts
- for (int i = 0; i < COUNTS_LEN; i++)
- if (neg_counts[i] >= 0 && neg_counts[i] % 2 != 0)
- return -i;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement