## Recommended Posts

I have an odd number of elements.

Only one is unique.

How do I find it?

I have this right now, but it only removes 1 duplicate, not two.

```#include &lt;iostream&gt;

using namespace std;
const int arrayLen = 13;

int main (int argc, char * const argv[])
{
int array[arrayLen]={0,0,1,1,2,2,3,3,4,4,5,6,6};
int i,t,j,k;

for(i = 1; i &lt; arrayLen; i++)
{
t += array[i];

for(i = 1, k = 1, j = i + 1; j &lt;= arrayLen; j++)
{
if (t - array[i] == t - array[j])
continue;

k++;
array[k] = array[j];
i = j;
}
}
for(i = 1; i &lt;= k; i++)
cout &lt;&lt; array[i] &lt;&lt;  " ";

}```

5 is the unique element here. I had something I wrote myself earlier, but I kept getting segmentation faults when I made my array larger than 11 and couldn't for the life of me, figure out why. I lifted this code from some website and am looking to alter it.

Thanks for the help.

EDIT: This is just an example, a demo..., it's going to be something bigger later. So it doesn't need to be overly complicated or anything.

##### Share on other sites

I'd make it create 2 arrays and each time you go over a unique value, add it to the array. In pseudo-code it might be something like this:

```Let x=0
Let y=0
Let z=0
Let invalid=0
Let array={0,0,1,1,2,2,3,3,4,4,5,6,6}
Let array2={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}

While x&lt;len
Do
While y&lt;len and array2[y]!=-1
Do
if array[x]==array2[y]
Then
Invalid=1
break
End
End
If invalid!=1
Then
array2[z]=array[x]
x=x+1
End
Invalid=0
x=x+1
End
x=0
While x&lt;len and array2[x]!=-1
Do
Print array2[x]
x=x+1
End```

Edit:

This will find all of the unique elements (or at least it should when written in the right syntax and stuff), if you want to just find one unique, you can have a break after you check that invalid is not equal to 1

##### Share on other sites

So something like this?

```#include &lt;iostream&gt;

using namespace std;
const int arrayLen = 13;

int main (int argc, char * const argv[])
{
int array[arrayLen]={0,0,1,1,2,2,3,3,4,4,5,6,6};
int unique[arrayLen]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int x = 0, y = 0, z = 0, invalid = 0;

while (x &lt; arrayLen)
{
while ((y &lt; arrayLen) &amp;&amp; (unique[y] != -1))
{
if(array[x] == unique[y])
{
invalid = 1;
cout &lt;&lt; endl &lt;&lt; "I'm stuck here" &lt;&lt; endl;
break;
}
}
if (invalid != 1)
{
unique[z] = array[x];
cout &lt;&lt; endl &lt;&lt; "Actually, I'm stuck here" &lt;&lt; endl;
x++;
break;
}
else
{
invalid = 0;
x++;
}

}

x = 0;
while ((x &lt; arrayLen) &amp;&amp; (unique[x] != -1))
{
cout &lt;&lt; endl &lt;&lt; "Do I even get here?" &lt;&lt; endl;
cout &lt;&lt; unique[x] &lt;&lt; " ";
x++;
}

}```

It doesn't seem to work...,

output:

```Actually, I'm stuck here

Do I even get here?
0 logout

[Process completed]```

EDIT: Thanks a lot for helping me, btw.

##### Share on other sites

```#include &lt;stdio.h&gt;

int main()
{
int array={0,0,1,1,2,2,3,3,4,4,5,6,6};
int array2={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int unique={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int v=0,w=0, x = 0, y = 0, z = 0, invalid = 0;

for(x=0;x&lt;13;x++) // array
{
for(y=0;y&lt;13;y++) // array2
{
if(array[x]==array2[y])
invalid=1;
}
if(invalid==0)
{
for(v=0;v&lt;13;v++)
{
if(unique[v]==array[x])
{
unique[v]=-1;
invalid=1;
}
}
if(invalid==0)
{
unique[w++]=array[x];
}
}
else
{
for(v=0;v&lt;13;v++)
{
if(unique[v]==array[x])
unique[v]=-1;
}
}
invalid=0;
}

for(x=0;x&lt;13;x++)
{
if(unique[x]!=-1)
printf("%d\n",unique[x]);
}
}```

There's what I have in C, I think I messed up on the pseudocode a lot.

Edit:

btw, I have no clue how it's even getting to the else statement since I'm not setting anything in array2[] or at least I don't think I am lol

##### Share on other sites

Well, It's woefully convoluted, but it works...,

Any way to trim the fat a bit?

##### Share on other sites

```#include &lt;stdio.h&gt;

int main()
{
int array={0,0,1,1,2,2,3,3,4,4,5,6,6};
int array2,unique={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int v,w,x,y,z,invalid=0;

for(x=0;x&lt;13;x++) // array
{
for(y=0;y&lt;13;y++) // array2
if(array[x]==array2[y] || array[x]==unique[y])
{
invalid=1;
if(unique[y]==array[x])
unique[y]=-1;
}
if(invalid==0)
unique[w++]=array[x];
invalid=0;
}

for(x=0;x&lt;13;x++)
if(unique[x]!=-1)
printf("%d\n",unique[x]);
}```

That should work as well. Much shorter :P

##### Share on other sites

Actually, I'm being told that it's possible with one variable, runs in linear time and takes advantage of the XOR operation.

Now I'm perplexed.

EDIT: And it doesn't have to be sorted either..., that's the biggie!

##### Share on other sites

```#include &lt;stdio.h&gt;

int main()
{
int x,y,invalid;
int array={0,0,1,1,2,2,3,3,4,4,5,6,6};
for(x=0;x&lt;13;x++)
{
for(y=0;y&lt;13;y++)
if(y!=x &amp;&amp; array[x]==array[y])
invalid=1;
if(invalid!=1)
printf("%d\n",array[x]);
invalid=0;
}
}```

That's about as short as I can get it and I can't think of a way to make it any different lol.

Editz0r:

Did you ask them for an example of how they could do that? I see no way of only using one variable with XOR :/.

##### Share on other sites

That's the riddle I guess.

I'm going crazy! The tantilizing hints don't help either!

##### Share on other sites

Yeah, really. Did you figure it out yet? I'm starting to wonder about it too! lol

Edit:

Ooooooooh, found it!!!! http://blog.arahul.in/2008/06/find-duplicate-element.html

Edit2:

But that only finds one non-duplicate element. That's what's wrong with it lol.

Edit3:

Also, if I'm thinking correctly, if there's a number that shows up an odd number of times, it won't get canceled out and end up being output as the non-duplicate element and if there's no non-duplicate elements, it will output 0 which in most cases is not desirable.

##### Share on other sites

Yeah, really. Did you figure it out yet? I'm starting to wonder about it too! lol

Edit:

Ooooooooh, found it!!!! http://blog.arahul.in/2008/06/find-duplicate-element.html

Edit2:

But that only finds one non-duplicate element. That's what's wrong with it lol.

Edit3:

Also, if I'm thinking correctly, if there's a number that shows up an odd number of times, it won't get canceled out and end up being output as the non-duplicate element and if there's no non-duplicate elements, it will output 0 which in most cases is not desirable.

You found it. This is the exact problem.

##### Share on other sites

Hmm, so I implemented it and it works flawlessly, but I still don't totally understand how it works. I get what xor does ( 01/10 = true 00/11 = false) but I don't get how it can eliminate elements on one pass without knowing where the other one is.

##### Share on other sites

Xor does it bit by bit with the binary.

xor 001 with 001:

001

^001

------

000

You xor bit by bit. The xor of an integer as:

```binary={0,0,0,0,0,0,0,1};
binary2={0,0,0,0,0,0,0,1};
int x;
for(x=0;x&lt;8;x++)
binary[x]^binary2[x];```

That's doing xor 1^1.

##### Share on other sites

How it works:

if you have two integers, x and y, x^y will return an integer such that each bit will be 0 if the corresponding bits from x and y are the same, and 1 if they are different. This leads to two properties: z = x^x == 0, because every bit is the same, and z = x^0 == x, because when nth bit of x is 1, it is different from 0, therefor the nth bit of z is 1, and when the nth bit of x is 0, it is the same as 0, therefor the nth bit of z is 0.

Ex:

5^4 = 0b101^0b100 = 0b001 = 1 <-- only the last bit is different

7^9 = 0b0111^0b1001 = 0b1110 = 14 <-- the first three bits are different

7^0 = 0b111^0b000 = 0b111 = 7 <--only the bits that are 1 (all three) are different

8^0 = 0b1000^0b0000 = 0b1000 = 8 <-- only the bits that are 1 (the first) are different

6^6 = 0b110^0b110 = 0b000 = 0 <-- no bits are different

So, if you traverse an array, and xor each item with the cumulative xor of the previous, any duplicates will cancel out:

```int nums = {1,1,2,2,3};
int i, x = 0;
for(i = 0; i &lt; 5; i++)
x = x^nums[i];
cout &lt;&lt; x &lt;&lt; endl;
```

results in:

x = 1^1^2^2^3 = (1^1)^(2^2)^3 = 0^0^3 = 0^3 = 3

Note: this will work so long as 1) there is only ONE unique number (otherwise, it will return the xor of all unique elements), and 2) any duplicate elements have an even number of elements (they cancel out in pairs, and any remaining numbers would 'pollute' the output)

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
• ### Recently Browsing   0 members

• No registered users viewing this page.
×