# Thread: Store only non-double elements

1. ## Store only non-double elements

Hi there, this is getting me frustrated and I'm trying to solve this to keep working on my code:

I have an array of integers whose elements may be equal:
ex.: [1, 3, 3, 4, 10];

I want to save, inside another array, only the elements that are different among eachother. I tag my code below; I can't get where I do it wrong, in fact, it does not recognize the different numbers.

Thank you in advance for helping

S.

Code:
``` //Get rid of the double node numbers:
s = 0;
for(i=0; i<2*nbnd_el; i++){
for(k=0; k<2*nbnd_el; k++){
if( _node[k] =! _node[i] ){
_NODE[s] = _node[i];
s = s+1;
printf("NODE = %d\n", _NODE[s]);
}
}
}```
p.s. This is not homework, I've been out from the Uni since 2005 already

2. Code:
`_node[k] =! _node[i]`
Are you sure you want to assign _node[k] with the value of !_node[i]?

--
Mats

3. Originally Posted by matsp
Code:
`_node[k] =! _node[i]`
Are you sure you want to assign _node[k] with the value of !_node[i]?

--
Mats
Hi mats, thanks for replying. No, what I am doing is checking whether any element of _node[] appears more than once in the array, and to do that I thought of running through the array element by element and checking if any element appears again inside the exact array copy _node[k] respect to _node[i]

Thanks
S.

4. So, what I was trying to say is that you are not testing correctly if the element is in the other array, but rather copying the result of "not _node[i]" into _node[k]. You will probably notice this as _NODE[s] never gets updated except when _node[i] is 0, as !_node[i] is only true when it's zero.

The correct way to check if _node[k] is not equal to _node[i] would be using the != operator.

--
Mats

5. Originally Posted by matsp
So, what I was trying to say is that you are not testing correctly if the element is in the other array, but rather copying the result of "not _node[i]" into _node[k]. You will probably notice this as _NODE[s] never gets updated except when _node[i] is 0, as !_node[i] is only true when it's zero.

The correct way to check if _node[k] is not equal to _node[i] would be using the != operator.

--
Mats
Hi Mats, Thanks again; actually I hadn't realized the syntax error in the use of the operator; thank you about that. However, I still obtain what I am not looking for, in fact, given my array as:

_node[] = [10 11 11 12 12 9 9 8 8 5];

I would like to obtain _NODE[] = [10 11 12 9 8 5], but the result that I get is the following:

NODE = 1
NODE = -1208836096
NODE = 1
NODE = -1209129760
NODE = -1209129760
NODE = -1082134028
NODE = -1210072491
NODE = -1209129760
NODE = -1208836096
NODE = 1
NODE = -1082134560
NODE = 1
NODE = -1208836096
NODE = -1209131020
NODE = 1
NODE = 10
NODE = 11
NODE = 11
NODE = 12
NODE = 12
NODE = 9
NODE = 9
NODE = 8
NODE = 8
NODE = 5
NODE = -1209131020

Why this?

Thank you,
Simone

6. If your current code is similar to the posted above, then you are printing the _NODE[s] AFTER you have moved to the next location, which leads to weird printing.

Also, I think your inner loop needs to look at ALL elements before deciding if it's in the second set or not, so you would want to move the assignment of the value _NODE[s] to outside of the "k" loop.

Also, you probably want to check if the value is in the _NODE array, rather than in the _node array. All values will be in the _node array at least once, as that's where you are getting the value from!

--
Mats

7. Originally Posted by matsp
If your current code is similar to the posted above, then you are printing the _NODE[s] AFTER you have moved to the next location, which leads to weird printing.

Also, I think your inner loop needs to look at ALL elements before deciding if it's in the second set or not, so you would want to move the assignment of the value _NODE[s] to outside of the "k" loop.

Also, you probably want to check if the value is in the _NODE array, rather than in the _node array. All values will be in the _node array at least once, as that's where you are getting the value from!

--
Mats
Hi there Mats, sorry for taking so long to reply; I was trying it in another way, but noithing; with your suggestion on the position of the print (as a check), I actually get to print it correctly, but the new array _NODE now correpsonds exactly to the original _node.

It is a very simple algorithm in principle: loop over every element, and save the value to a new array only if the element does not repeat; nevertheless, I cannot get it to work correctly.

Sorry for asking again, but would you have another suggestion?

All the best
Simone

8. I thought I had already explained that the current code is broken:
Code:
```For every element i in _node
for every element k in _node
if element k is not equal to element i
Code:
```for every element i in _node
for every element k in _NODE
if element k is equal to element i
f = 1; break;
if (!f) insert into _NODE.```
--
Mats

9. Originally Posted by matsp
I thought I had already explained that the current code is broken:
Code:
```For every element i in _node
for every element k in _node
if element k is not equal to element i
Code:
```for every element i in _node
for every element k in _NODE
if element k is equal to element i
f = 1; break;
if (!f) insert into _NODE.```
--
Mats
Thank you veyr much , I really appreciate!

I let you how that goes and post the final algorithm in the forum
All the best
Simone