473,373 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 software developers and data experts.

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.

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
11 12076
dmjpro
2,476 2GB
@cmb3587
On the basis of which logic you are saying the difference ?
Apr 20 '09 #2
JosAH
11,448 Expert 8TB
@dmjpro
From ordinary set theory: A-B are all elements in A that are not in B.

kind regards,

Jos
Apr 20 '09 #3
r035198x
13,262 8TB
Why not have a look at the ArrayList class and see what magic you can conjure up with it?
Apr 20 '09 #4
dmjpro
2,476 2GB
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
r035198x
13,262 8TB
@dmjpro, if you keep looking at the API you will find cleaner and even easier ways of doing it.
Apr 20 '09 #6
dmjpro
2,476 2GB
@r035198x
Sorry i couldn't find it any? ;)
Apr 20 '09 #7
r035198x
13,262 8TB
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
2,476 2GB
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
JosAH
11,448 Expert 8TB
@r035198x
I don't understand the 'blatant cheaters' part ...

kind regards,

Jos ;-)
Apr 20 '09 #10
r035198x
13,262 8TB
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
JosAH
11,448 Expert 8TB
@r035198x
<simply splats r035198x under the sole of his shoe/>

kind regards,

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

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

Similar topics

2
by: duyifan.nju | last post by:
hello, can someone tell me what is the difference between sort() (in stl) & qsort(stdlib.h) thanks in advance.
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
5
by: Alan Howard | last post by:
We're getting "ERROR (0x8007000E) Not enough storage is available to complete this operation" errors on a fairly large, busy ASP/SQL Server web site. The error is being thrown on a line calling...
18
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
14
by: code break | last post by:
what is the difference in this pointers decalarition ? int *ptr; and int (*ptr);
2
by: benj | last post by:
Anybody has a good explaination to following question? what is the difference between: int *a and int (*a)
45
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
10
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
3
by: Jeff | last post by:
I've got a series of data like this: Long Sleeve White P/C Sm 32/33 Long Sleeve White P/C Med 32/33 .... What I'd like to do is extract the differences and the similarity. In this case: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.