Array Element Parity
Given an array of integers where each integer except one appears exactly twice (once positive, once negative), find the integer that has no corresponding negative/positive counterpart. This integer could be zero, for which the rule still applies: it should appear twice if valid. Your task is to find and return this single integer.
[ 1, -1, 2, -2, 3 ]
Explanation. All numbers except 3 have their negative counterparts.
[ -4, 4, 7, -7, 6, -6, 0 ]
Explanation. All numbers have their counterparts, even zero appears as required.
[ -1, 1, -2, 2, 0, 0, 5 ]
Explanation. 5 is the only number without a negative counterpart.
Follow-up: What would be the space complexity if the problem constraint was relaxed to integers appearing 'n' times, still requiring to find one that appears 'n - 1' times?
1. The array will contain at least one element.\n2. Every integer in the array, except one, will appear exactly twice: once as a positive number and once as its negative counterpart.
- Views
- 2