Find a unique integer in an array

I am looking for an algorithm to solve the following problem: we are given an integer array of size n that contains k (0 <k <n) a set of elements exactly once. Each other integer occurs an even number of times in the array. The output must be any of k unique numbers. k is a fixed number, not part of the input.

An example is an input [1, 2, 2, 4, 4, 2, 2, 3]in which both 1 and 3 are the correct output.

Most importantly, the algorithm must work in O (n) time and requires only O (1) additional space.

edit: there was some confusion as to whether there was only one unique integer or more. I apologize for this. The correct problem is that there is an arbitrary but fixed amount. I updated the original question above.

"Dante." gave a good answer to the case that there are not more than two such numbers. This link also offers a solution for three. "David Eisenstat" commented that it is also possible to do this for any fixed k. I would appreciate a solution.

+4
source share
5 answers

-, k, , , O (nk) (: , k O (1)) O (1) , , " " " 1 , (popcount)", . , , ( , ), , .

. , , , . XOR ; , , , . , k .

. x w (, w). A w ceil (1 + lg k) b ceil (1 + lg k). , x, Ax = b, mod 2. A ceil (1 + lg k) a1, a2, .... Ax popcount(a1 ^ x), popcount(a2 ^ x), .... ( , b, lg k .)

, , . , x , Ax = b 2 -ceil (1 + lg k)= & Theta; ( 1/). , Ax = b, y ≠ x , Ay = b 2 -ceil (1 + lg k). , , x, 1/2, 1/2, x . ( ), & Theta; (1).


- k = 3. a, b, c. XOR , s = a ^ b ^ c. i , a[i] == b[i] == c[i], s[i] == a[i] == b[i] == c[i]. , XOR , s ^ x. . . , , XOR, s. , , k = 2 .

+3

XOR:

= O (n)

= O (1)

, , , , :

, 0 1 , 0 xor.

0^1^....... = 0 as long as number of 0 is even and number of 1 is even 

0 1 .

, , , 1 0 , , , , xor

0(from no occuring even times)^1(from no occuring once) = 1 

0(from no occuring even times)^0(from no occuring once) = 0

, , , .

, xor , , .

, n:

 result = array[0]^array[1]^.....array[n-1] 

OP , , , , .

, , .

:

xor , , , , , 0, :

1 , , , .

.

xor, 1 ( , 1), 0. , ().

1, , , , 0, 1. , , , 0, .

, xor

 A = result & ~(result-1)

, array [i] & A 0, number_1

 number_1 = number_1^array[i]

 number_2 = number_2^array[i]

, .

,

1. xor , xor.

2. xor B.

3. :

number_1=0,number_2=0;
for(i = 0 to n-1)
{
 if(array[i] & B)
  number_1 = number_1^array[i];
 else
  number_2 = number_2^array[i];
}

_1 number_2 .

+8

" ", , .

: XOR . , ( 2), .

. , . .

, k k sum(a[i]**k). .. a [i], a [i] 2 .. , , ?, , . IDK, xor , .. .

+3

1. -. , -, . O (n), , , .

Javascript jsfiddle http://jsfiddle.net/nmckchsa/

function findUnique(arr) {
    var uniq = new Map();
    var result = new Set();
    // iterate through array
    for(var i=0; i<arr.length; i++) {
        var v = arr[i];
        // add value to map that contains counts
        if(uniq.has(v)) {
            uniq.set(v, uniq.get(v) + 1);
            // count is greater than 1 remove from set
            result.delete(v);
        } else {
            uniq.set(v, 1);
            // add a possibly uniq value to the set
            result.add(v);
        }
    }
    // set to array O(n)
    var a = [], x = 0;
    result.forEach(function(v) { a[x++] = v; });
    return a;
}
alert(findUnique([1,2,3,0,1,2,3,1,2,3,5,4,4]));

EDIT. uniqq , @PeterCordes .

.

function findUnique(arr) {
    var result = new Set();
    // iterate through array
    for(var i=0; i<arr.length; i++) {
        var v = arr[i];
        if(result.has(v)) { // even occurances
            result.delete(v);
        } else { // odd occurances
            result.add(v);
        }
    }
    // set to array O(n)
    var a = [], x = 0;
    result.forEach(function(v) { a[x++] = v; });
    return a;
}

JSFiddle http://jsfiddle.net/hepsyqyw/

+1
  • counts INT_MAX , .

  • , increment counts[element] . (: counts[element] = (counts_element+1)%2, N. , )

  • counts, , "1". .

2 - O (N). 1 3 , , - O (1).


( : , , .)

-3

Source: https://habr.com/ru/post/1606960/


All Articles