If are you looking for Union of Two Arrays using Primitive Data Types
then visit this.
In this example I’m using Java Collection Class TreeSet. This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time
cost for the basic operations (add, remove and contains).
Note that the ordering maintained by a set
(whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface (See Comparable or Comparator for a precise definition of consistent with equals.)
This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
Java Code:
package com.crunchify.tutorials; /** * @author Crunchify.com */ import java.util.TreeSet; public class CrunchifyUnionTwoArraysWithCollection { public static void main(String[] args) { Integer[] arrayOne = { 4, 11, 2, 1, 3, 3, 5, 7 }; Integer[] arrayTwo = { 5, 2, 3, 15, 1, 0, 9 }; Integer[] union = findUnion(arrayOne, arrayTwo); System.out.println("\nUnion of Two Arrays: "); for (Integer entry : union) { System.out.print(entry + " "); } } public static Integer[] findUnion(Integer[] arrayOne, Integer[] arrayTwo) { // TreeSet<Integer> hashedArray = new TreeSet<Integer>(); for (Integer entry : arrayOne) { hashedArray.add(entry); } for (Integer entry : arrayTwo) { hashedArray.add(entry); } return hashedArray.toArray(new Integer[0]); } }
Output:
Union of Two Arrays: 0 1 2 3 4 5 7 9 11 15