Java ProgramsArraysFind Union of Arrays

Find Union of Arrays in Java

intermediate·  Arrays  ·  Array

Problem

The union of two arrays is every distinct value that appears in at least one of them, with no duplicates and no double-counting of values shared by both.

Given two arrays, find every distinct element present in either one.

Input
[3, 6, 9, 12, 15], [6, 12, 18, 24]
Output
Union: 3, 6, 9, 12, 15, 18, 24

Java Program

Java
import java.util.LinkedHashSet; import java.util.Set; public class UnionOfArrays { public static void main(String[] args) { int[] arr1 = {3, 6, 9, 12, 15}; int[] arr2 = {6, 12, 18, 24}; Set<Integer> union = new LinkedHashSet<>(); for (int num : arr1) union.add(num); for (int num : arr2) union.add(num); // duplicates like 6 and 12 are silently ignored StringBuilder result = new StringBuilder(); for (int num : union) { if (result.length() > 0) result.append(", "); result.append(num); } System.out.println("Union: " + result); } }

Output

Union: 3, 6, 9, 12, 15, 18, 24

Core Logic

Adding every element from both arrays into a LinkedHashSet automatically discards duplicates while preserving the order elements were first seen.

How It Works
  1. 1A LinkedHashSet<Integer> named union starts empty.
  2. 2Every element of arr1 is added first, in order.
  3. 3Every element of arr2 is then added — a value already present, like 6 or 12, is silently ignored since Set.add() doesn't add duplicates.
  4. 4The final set holds every distinct value from both arrays, in the order each was first encountered.
For [3, 6, 9, 12, 15] and [6, 12, 18, 24], adding arr1 first gives 3, 6, 9, 12, 15, then adding arr2 only contributes the new values 18 and 24.
💡

Key Point: This is almost the same code as the intersection program — the difference is entirely in what gets kept: intersection keeps only shared values, union keeps everything with duplicates merged.

Complexity
Time Complexity: O(n + m)Space Complexity: O(n + m)

Why: Every element from both arrays is added once, and the set can hold up to n + m distinct values in the worst case where nothing overlaps.

Key Concepts

LinkedHashSetinsertion orderfor-each loop

Approach 2: Java 8

Java
import java.util.Arrays; import java.util.stream.Collectors; import java.util.stream.IntStream; public class UnionOfArraysStream { public static void main(String[] args) { int[] arr1 = {3, 6, 9, 12, 15}; int[] arr2 = {6, 12, 18, 24}; // Chains both arrays into one stream, then drops repeated values String union = IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2)) .distinct() .mapToObj(String::valueOf) .collect(Collectors.joining(", ")); System.out.println("Union: " + union); } }

Output

Union: 3, 6, 9, 12, 15, 18, 24

Core Logic

Concatenating both arrays into a single stream, then filtering out duplicates, builds the union in one declarative pipeline.

How It Works
  1. 1IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2)) chains both arrays into a single stream, arr1's elements followed by arr2's.
  2. 2.distinct() keeps only the first occurrence of each value across the whole combined stream.
  3. 3.toArray() collects the deduplicated values back into a plain int[].
Concatenating [3, 6, 9, 12, 15] and [6, 12, 18, 24] gives a nine-element stream; distinct() trims it down to the same seven-element union.
💡

Key Point: distinct() here plays the same role as the manual version's LinkedHashSet — both track every value already seen across the combined input.

Complexity
Time Complexity: O(n + m)Space Complexity: O(n + m)

Why: distinct() tracks every value it's already seen across the concatenated stream, and the result array holds up to n + m distinct elements.

Key Concepts

StreamIntStream.concat()distinct()

Related Programs