apsasha
apsasha

Reputation: 73

Create a method that with 2 given arrays returns a new one with elements from a and b with no duplicates:

Hey everyone I am a beginner and I am trying to do this problem, here is what I have tried so far...I am really stumped though:

Write a method called int[] arrayUnion(int[] a, int[] b) that, given two int arrays, returns a new array with unique elements from a and b. No duplicates!

Example Outputs: arrayUnion([1, 2, 3], [-1, 0, 1]) returns [1, 2, 3, -1, 0]

arrayUnion([1, 1, 1], [1, 1, 1]) returns [1]

   public static int[] arrayUnion(int[] a, int[] b){
        int[] result;

        for(int i =0; i< a.length; i++){
            for(int k =0; k<result.length; k++){
                if(a[i]!= result[k]){
                    result.add(a[i]);
                }
            }
        }


         for(int j =0; j< b.length; j++){
            for(int k =0; k<result.length; k++){
                if(b[j]!=result[k]){
                  result.add(b[j]);
                }
            }
        }
        return result;
    }

Upvotes: 0

Views: 728

Answers (3)

flaxel
flaxel

Reputation: 4587

First you can not use the add method for an array. So therefore your code could look like this:

public int[] arrayUnion(int[] a, int[] b) {
    List<Integer> result = new ArrayList<>();

    for (int value: a) {
        if (!result.contains(value)) {
            result.add(value);
        }
    }

    for (int value: b) {
        if (!result.contains(value)) {
            result.add(value);
        }
    }

    return result.stream().mapToInt(Integer::intValue).toArray();
}

Second it is a little bit easier with a set. Therefore you can easily add all values and return the array:

public int[] arrayUnion(int[] a, int[] b) {
    Set<Integer> result = new HashSet<>();

    for (int value: a) {
        result.add(value);
    }

    for (int value: b) {
        result.add(value);
    }

    return result.stream().mapToInt(Integer::intValue).toArray();
}

And there is also a third option. You can use the Java Stream API with the flatMap:

public int[] arrayUnion(int[] a, int[] b) {
    return Stream.of(a, b).flatMapToInt(IntStream::of).distinct().toArray();
}

Upvotes: 0

WJS
WJS

Reputation: 40034

You can do it with a stream, merging the two arrays keeping only the distinct values single array.

int[] a = { 1, 2, 3, 2, 7, 4, 5 };
int[] b = { 2, 9, 4, 5, 5 };

int[] union = arrayUnion(a,b);
System.out.println(Arrays.toString(union));
 

prints

[1, 2, 3, 4, 5, 7, 9]   

This works by

  • flattening the two arrays into an Intstream.
  • getting rid of duplicates
  • and converting back to an int array.
static int[] arrayUnion(int[] a, int[] b) {
  return Stream.of(a,b).flatMapToInt(IntStream::of)
     .distinct()
     .toArray();
}

Upvotes: 2

Sikakollu
Sikakollu

Reputation: 56

If you can use the Java Collections, you can use Set as follows to get the unique integers array:

public static int[] arrayUnion(int[] a, int[] b){

        HashSet<Integer> set=new HashSet();

        for(int i =0; i< a.length; i++){
            set.add(a[i]);  
        }

        for(int i =0; i< b.length; i++){
            set.add(b[i]);  
        }

        int n = s.size(); 
        int[] result; = new String[n]; 
  
        int j = 0;
        for(Integer i: set) {
            result[j++] = i;
        }

        return result;
}

Upvotes: 0

Related Questions