Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
hi i need to get union of two set in java(based on set theory)
for example A={1,2,3,4,5}
B={1,2,3,6,7}
UNION:C= {1,2,3,4,5,6,7}

i coded a way but the result is wrong
plz help me!
my code:
public char[] union(char a[],char b[],int n,int m){
    int count=0;
    for(int i=0;i<a.length;i=i+2){
       for(int j=0;j<b.length;j=j+2){
       if(a[i]==b[j]){
           ++count;
       }}}
      c=new char [(n*2+m*2)-count*2];
 int j=0,h=0;
 for(int i=0;i<a.length;++i){
     c[i]=a[i];
     ++j;}
  
       for(;j<c.length;j=j+2){
           for(int i=0;i<a.length;i=i+2){
               for(int k=0;k<b.length;k=k+2){
               if(c[i]==b[k]){
                   continue;}
           else
              c[j]= b[k];  }  }
                   
   }
 return c;
Posted

You could try something like this:

Java
public class Program {

    private static boolean contains(char[] array, char value) {
        for(char item : array) {
            if (item == value)
                return true;
        }
        return false;
    }

    public static char[] union(char[] a, char[] b) {

        List<Character> result = new ArrayList<Character>();

        for(char value : a) {
            if (contains(b, value)) {
                result.add(value);
            }
        }

        char[] resultArray = new char[result.size()];
        for(int i = 0; i < result.size(); ++i) {
            resultArray[i] = result.get(i);
        }

        return resultArray;
    }


    public static void main(String[] args) {

        char a[] = new char[] { 1, 2, 3, 4, 5};
        char b[] = new char[] { 1, 2, 3, 6, 7};

        for(char c : union(a, b)) {
            System.out.print((int)c);
        }
    }

}



/Fredrik
 
Share this answer
 
Comments
DominoBoy 27-May-11 6:49am    
thx a lot
Fredrik Bornander 27-May-11 8:28am    
Glad I could help.
You should mark my answer as accepted if it solved your problem so that we can close the question.
Why don't you make use of the Available property of java to perform the Union Operation ?

Follow the Below Steps :

* Import java.uitl.*

* Declare 2 Sets using the below methods.
 Set setName = new HashSet();
setName.add("value"); 


* Find the Union Using the below method
 Set unionSetName = new TreeSet(setName1);
unionSetName.addAll(setName2);


* Print the Values in the Union Set using Iterator
 Iterator it = unionSetName.iterator();
while(it.hasNext())
{
   System.out.println(in.next());
}


Take a Look at the below link for the Documentation of Set interface.
Set Interface[^]


BR//
Harsha
 
Share this answer
 
v2
Comments
DominoBoy 27-May-11 6:42am    
yeah i know but its my school work to get union of tow set without using java Set
thx any way..
The best way to solve this problem is to use addAll[^] method of Set<T>: add elements of a and b to HashSet<char>, then call aSet.addAll(bSet); If you need the result to be sorted, use TreeSet instead of HashSet.

P.S. Your code is grossly wrong: you are skipping odd-numbered elements for some reason (you do i = i+2 instead of i++ everywhere). If you insist on doing the union in your own code, sort both arrays before you do it: after that, you can merge everything in a single loop.
 
Share this answer
 
Comments
DominoBoy 27-May-11 6:44am    
its my school work to get union of tow set without using java Set thx any way..
my code is for both numbers and char so how can i sort them?
dasblinkenlight 27-May-11 9:17am    
Use Arrays.sort() method to sort the arrays.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900