Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Homework
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 19-Nov-12 5:41am
lamogha106
Edited 19-Nov-12 23:05pm
TorstenH.33.8K
v2
Comments
Nelek at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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]
 
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]
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100