Click here to Skip to main content
Rate this: bad
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
Edited 19-Nov-12 23:05pm
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
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;
if (error == false)
   // No match found, do whatever you need to
[edit] array[b] corrected to array_b[j] [/edit]

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

Advertise | Privacy | Mobile
Web01 | 2.8.150302.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2015
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