On 15 Jun, 13:55, Umesh <fraternitydispo...@gmail.comwrote:
How can I extract common elements of two number arrays into another
array?
// the following doesn't work
for(k=0;k<10;k++)
for(l=0;l<15;l++)
if(p[k]==q[l]) {r[m]=p[k];++m;}
This code looks OK to me, as long as you set m to zero beforehand and
r is big enough. The one snag I can see is that, if an element occurs
more than once in a list, and at least once in the other, it may be
selected in r more times than you want. One way round that might be to
change the value in p to some sort of non-value (eg you could use 0,
or -1, if this wasn't going to occur in the real data). An alternative
might be to have another array to indicate whether the value in p had
been "used" yet or not. It's up to you.
Hope this helps.
Paul.