Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i compare characters of two arrays before adding elements from one of the arrays into the other, inorder to ensure that if the characters already exist in the destination array, then that character should not be copied into it?
Posted
Updated 19-Nov-12 22:05pm
v2
Comments
Nelek 19-Nov-12 11:21am    
What type of data are you actually speaking about? How about the length of the arrays / volumen of data?

1 solution

As far as the arrays are not too long and that the type of data can be compared with == operator

You can use two for loops one nested into the another one. Actually the same operation than going through a 2-dimensional array[i][j]

C#
bool error = false;
for (int i=0; i < length_a; i++)
{
   for (int j=0; j < length_b; j++)
   {
      if (array_a[i]== array[b] array_b[j])
      {
         error = true;
         break;
      }
   }
}

if (error == false)
{
   // No match found, do whatever you need to
}


[edit] array[b] corrected to array_b[j] [/edit]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900