By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,180 Members | 1,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,180 IT Pros & Developers. It's quick & easy.

difference of 2 arrays

P: 5
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays

Ex:

arrayA: 3 5 8 9
arrayB: 3 4 6 9

difference of A-B: 5 8

however, my code is just returning me an array of 0's

im trying to use my search method (which has been tested and works) to take a number from the first array and search it in the other array. If it is not found the search method returns a 0 and the number from the first array is then stored into the 3rd array.

here is the portion of my code, I'm not sure what is wrong.

Expand|Select|Wrap|Line Numbers
  1. //Cory Boughton
  2.  
  3. import java.util.*;
  4.  
  5. public class Sets {
  6.  
  7.     public static Scanner kbd = new Scanner (System.in);
  8.  
  9.     public static final int MAXSIZE = 20;
  10.  
  11.     public static void main(String[] args) {
  12.  
  13.         int[]setA = new int[MAXSIZE];
  14.         int[]setB = new int[MAXSIZE];
  15.         int[]intersect = new int [MAXSIZE];
  16.         int[]difference = new int [MAXSIZE];
  17.         int sizeA, sizeB, interSize, diffSize;
  18.  
  19.         System.out.print("How many numbers will be in the 1st set: ");
  20.         sizeA = kbd.nextInt();
  21.         while (sizeA > MAXSIZE){
  22.             System.out.print("Error: Set size is too large. Re-enter set size: ");
  23.             sizeA = kbd.nextInt();
  24.         }
  25.         System.out.println("Enter list of integers for 1st set: ");
  26.         getData(setA, sizeA);
  27.  
  28.         System.out.print("How many numbers will be in the 2nd set: ");
  29.         sizeB = kbd.nextInt();
  30.         while (sizeB > MAXSIZE){
  31.             System.out.print("Error: Set size is too large. Re-enter set size: ");
  32.             sizeB = kbd.nextInt();
  33.         }
  34.         System.out.println("Enter list of integers for 2nd set: ");
  35.         getData(setB, sizeB);
  36.  
  37.         interSize = intersection(setA, sizeA, setB, sizeB, intersect);
  38.         System.out.print("The intersection of the two sets is: ");
  39.         for (int x = 0; x < interSize; x++){
  40.             System.out.print(intersect[x] + " ");
  41.         }
  42.  
  43.         diffSize = difference(setA, sizeA, setB, sizeB, intersect);
  44.         System.out.print("\n\nThe difference of A-B is: ");
  45.         for (int x = 0; x < diffSize; x++){
  46.             System.out.print(difference[x] + " ");
  47.         }
  48.     }
  49.  
  50.     public static void getData(int[]set, int size){
  51.  
  52.         for(int x = 0; x < size; x++){
  53.             int num = kbd.nextInt();
  54.             int count = search(set, size, num);
  55.             if (count == 0)
  56.                 set[x] = num;
  57.             else 
  58.                 x--;
  59.         }
  60.     }
  61.  
  62.     public static int search(int[]set, int size, int num){
  63.  
  64.         int count = 0;
  65.  
  66.         for (int x = 0; x < size; x++){
  67.             if (num == set[x])
  68.                 count++;
  69.         }
  70.         return count;
  71.     }
  72. public static int difference(int[]setA, int sizeA, int[]setB, int sizeB, int[]resultSet){
  73.  
  74.         int y = 0;
  75.         for (int x = 0; x < sizeA; x++){
  76.             int num = setA[x];
  77.             int found = search(setB, sizeB, num);
  78.             if (found == 0){
  79.                 resultSet[y] = num;
  80.                 y++;
  81.             }
  82.         }
  83.         return y;
  84.     }
  85. }
  86.  
Apr 19 '09 #1
Share this Question
Share on Google+
11 Replies


dmjpro
100+
P: 2,476
@cmb3587
On the basis of which logic you are saying the difference ?
Apr 20 '09 #2

Expert 10K+
P: 11,448
@dmjpro
From ordinary set theory: A-B are all elements in A that are not in B.

kind regards,

Jos
Apr 20 '09 #3

10K+
P: 13,264
Why not have a look at the ArrayList class and see what magic you can conjure up with it?
Apr 20 '09 #4

dmjpro
100+
P: 2,476
See you better to use Collection instead of Array.
If you use Collection then you will find better APIs to do your work easily.

Expand|Select|Wrap|Line Numbers
  1. List set_a;
  2. List set_b;
  3. List set_c;
  4.  
  5. for(int i=0;i<set_a.size();i++){
  6.  if(set_b.indexOf(set_a.get(i))!=-1) set_c.add(set_a.get(i));
  7. }
  8.  
Apr 20 '09 #5

10K+
P: 13,264
@dmjpro, if you keep looking at the API you will find cleaner and even easier ways of doing it.
Apr 20 '09 #6

dmjpro
100+
P: 2,476
@r035198x
Sorry i couldn't find it any? ;)
Apr 20 '09 #7

10K+
P: 13,264
For starters you could have used the contains method.

For blatant cheaters you could look at retainAll combined with removeAll.
Apr 20 '09 #8

dmjpro
100+
P: 2,476
Ahh! actually "retainAll" method stroke my brain.
Anyway what i could figure out ...

Expand|Select|Wrap|Line Numbers
  1. List set_a;
  2. List set_b;
  3.  
  4. List temp_set = set_a.clone();
  5. temp_set.retainAll(set_b); //it finds out the common elements between two sets.
  6. set_a.removeAll(temp_set); //now the final desired set is ready
  7.  
That's what you said.
When do you make method call then i think lots of headache is up to RunTime System. What you think which one would be the better solution?
Apr 20 '09 #9

Expert 10K+
P: 11,448
@r035198x
I don't understand the 'blatant cheaters' part ...

kind regards,

Jos ;-)
Apr 20 '09 #10

10K+
P: 13,264
They are easy to spot. They start with a "J" and end with ... <starts running> .... ... ... o ... ... s ... <runs out of breath>
Apr 20 '09 #11

Expert 10K+
P: 11,448
@r035198x
<simply splats r035198x under the sole of his shoe/>

kind regards,

Jos (<--- never heard of the bastard ;-)
Apr 20 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.