difference of 2 arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmb3587
    New Member
    • Mar 2009
    • 5

    #1

    difference of 2 arrays

    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.

    Code:
    //Cory Boughton
    
    import java.util.*;
    
    public class Sets {
    	
    	public static Scanner kbd = new Scanner (System.in);
    	
    	public static final int MAXSIZE = 20;
    
    	public static void main(String[] args) {
    		
    		int[]setA = new int[MAXSIZE];
    		int[]setB = new int[MAXSIZE];
    		int[]intersect = new int [MAXSIZE];
    		int[]difference = new int [MAXSIZE];
    		int sizeA, sizeB, interSize, diffSize;
    		
    		System.out.print("How many numbers will be in the 1st set: ");
    		sizeA = kbd.nextInt();
    		while (sizeA > MAXSIZE){
    			System.out.print("Error: Set size is too large. Re-enter set size: ");
    			sizeA = kbd.nextInt();
    		}
    		System.out.println("Enter list of integers for 1st set: ");
    		getData(setA, sizeA);
    		
    		System.out.print("How many numbers will be in the 2nd set: ");
    		sizeB = kbd.nextInt();
    		while (sizeB > MAXSIZE){
    			System.out.print("Error: Set size is too large. Re-enter set size: ");
    			sizeB = kbd.nextInt();
    		}
    		System.out.println("Enter list of integers for 2nd set: ");
    		getData(setB, sizeB);
    		
    		interSize = intersection(setA, sizeA, setB, sizeB, intersect);
    		System.out.print("The intersection of the two sets is: ");
    		for (int x = 0; x < interSize; x++){
    			System.out.print(intersect[x] + " ");
    		}
    		
    		diffSize = difference(setA, sizeA, setB, sizeB, intersect);
    		System.out.print("\n\nThe difference of A-B is: ");
    		for (int x = 0; x < diffSize; x++){
    			System.out.print(difference[x] + " ");
    		}
    	}
    
    	public static void getData(int[]set, int size){
    		
    		for(int x = 0; x < size; x++){
    			int num = kbd.nextInt();
    			int count = search(set, size, num);
    			if (count == 0)
    				set[x] = num;
    			else 
    				x--;
    		}
    	}
    	
    	public static int search(int[]set, int size, int num){
    		
    		int count = 0;
    		
    		for (int x = 0; x < size; x++){
    			if (num == set[x])
    				count++;
    		}
    		return count;
    	}
    public static int difference(int[]setA, int sizeA, int[]setB, int sizeB, int[]resultSet){
    		
    		int y = 0;
    		for (int x = 0; x < sizeA; x++){
    			int num = setA[x];
    			int found = search(setB, sizeB, num);
    			if (found == 0){
    				resultSet[y] = num;
    				y++;
    			}
    		}
    		return y;
    	}
    }
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by cmb3587
    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
    On the basis of which logic you are saying the difference ?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by dmjpro
      On the basis of which logic you are saying the difference ?
      From ordinary set theory: A-B are all elements in A that are not in B.

      kind regards,

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Why not have a look at the ArrayList class and see what magic you can conjure up with it?

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          See you better to use Collection instead of Array.
          If you use Collection then you will find better APIs to do your work easily.

          Code:
          List set_a;
          List set_b;
          List set_c;
          
          for(int i=0;i<set_a.size();i++){
           if(set_b.indexOf(set_a.get(i))!=-1) set_c.add(set_a.get(i));
          }

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            @dmjpro, if you keep looking at the API you will find cleaner and even easier ways of doing it.

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Originally posted by r035198x
              @dmjpro, if you keep looking at the API you will find cleaner and even easier ways of doing it.
              Sorry i couldn't find it any? ;)

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                For starters you could have used the contains method.

                For blatant cheaters you could look at retainAll combined with removeAll.

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  Ahh! actually "retainAll" method stroke my brain.
                  Anyway what i could figure out ...

                  Code:
                  List set_a;
                  List set_b;
                  
                  List temp_set = set_a.clone();
                  temp_set.retainAll(set_b); //it finds out the common elements between two sets.
                  set_a.removeAll(temp_set); //now the final desired set is ready
                  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?

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by r035198x
                    For blatant cheaters you could look at retainAll combined with removeAll.
                    I don't understand the 'blatant cheaters' part ...

                    kind regards,

                    Jos ;-)

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      They are easy to spot. They start with a "J" and end with ... <starts running> .... ... ... o ... ... s ... <runs out of breath>

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by r035198x
                        They are easy to spot. They start with a "J" and end with ... <starts running> .... ... ... o ... ... s ... <runs out of breath>
                        <simply splats r035198x under the sole of his shoe/>

                        kind regards,

                        Jos (<--- never heard of the bastard ;-)

                        Comment

                        Working...