Find Union of Arrays in Java
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.
Java Program
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
Core Logic
Adding every element from both arrays into a LinkedHashSet automatically discards duplicates while preserving the order elements were first seen.
- 1A
LinkedHashSet<Integer>namedunionstarts empty. - 2Every element of
arr1is added first, in order. - 3Every element of
arr2is then added — a value already present, like6or12, is silently ignored sinceSet.add()doesn't add duplicates. - 4The final set holds every distinct value from both arrays, in the order each was first encountered.
[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.
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
Approach 2: Java 8
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
Core Logic
Concatenating both arrays into a single stream, then filtering out duplicates, builds the union in one declarative pipeline.
- 1
IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2))chains both arrays into a single stream,arr1's elements followed byarr2's. - 2
.distinct()keeps only the first occurrence of each value across the whole combined stream. - 3
.toArray()collects the deduplicated values back into a plainint[].
[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.
Why: distinct() tracks every value it's already seen across the concatenated stream, and the result array holds up to n + m distinct elements.