Merge Two String Arrays and Produce Unique Values using Java

Unique elements by merging arrays

In this tutorial, I will show you how to merge string values from two arrays and produce the final array with unique string values. I will use Set data structure to produce the unique results in final array.

When passed two arrays of strings, it will return an array containing the string values that appear in either or both arrays. The returned array should have no duplicates.

For example, calling mergeArrays(new String[]{'Ava', 'Emma', 'Olivia'}, new String[]{'Olivia', 'Sophia', 'Emma'}) should return an array containing Ava, Emma, Olivia, and Sophia in any order.

Prerequisites

Knowledge of Java

Merge Arrays and Produce Unique Elements

Now I will write Java code to merge two arrays to return the final array with unique elements.

I have implemented three ways to return the array with unique elements. Using Java 8’s Stream API with flatMap() and collect() methods to merge two array elements and finally return the unique elements using Set interface. Other two approaches uses simple iterations over elements from two arrays to merge and find the unique elements.

public class ArrayMergeUniqueValues {

	public static void main(String[] args) {
		String[] arr1 = new String[] { "Ava", "Emma", "Olivia" };
		String[] arr2 = new String[] { "Olivia", "Sophia", "Emma" };

		String[] arr = mergeArraysUnique1(arr1, arr2);

		System.out.println(Arrays.toString(arr));

		arr = mergeArraysUnique2(arr1, arr2);
		System.out.println(Arrays.toString(arr));

		arr = mergeArraysUnique3(arr1, arr2);
		System.out.println(Arrays.toString(arr));
	}

	public static String[] mergeArraysUnique1(String[] names1, String[] names2) {
		List<String> strings = Stream.of(names1, names2).flatMap(Stream::of).collect(Collectors.toList());

		Set<String> uniqueNames = new HashSet<String>(strings);

		String[] arr = new String[uniqueNames.size()];

		uniqueNames.toArray(arr);

		return arr;
	}

	public static String[] mergeArraysUnique2(String[] arr1, String[] arr2) {
		List<String> list = new ArrayList<>(Arrays.asList(arr1));
		list.addAll(Arrays.asList(arr2));

		Set<String> set = new HashSet<String>();
		for (String x : list) {
			set.add(x);
		}

		String[] arr = new String[set.size()];
		set.toArray(arr);

		return arr;
	}

	public static String[] mergeArraysUnique3(String[] arr1, String[] arr2) {
		String[] arr = new String[arr1.length + arr2.length];
		int count = 0;

		for (int i = 0; i < arr1.length; i++) {
			arr[i] = arr1[i];
			count++;
		}

		for (int j = 0; j < arr2.length; j++) {
			arr[count++] = arr2[j];
		}

		List<String> list = Arrays.asList(arr);

		Set<String> set = new HashSet<String>();
		for (String x : list) {
			set.add(x);
		}

		arr = new String[set.size()];
		set.toArray(arr);

		return arr;
	}

}

Testing Merge and Unique Elements

Executing the above code will give you the following output:

[Olivia, Ava, Emma, Sophia]
[Olivia, Ava, Emma, Sophia]
[Olivia, Ava, Emma, Sophia]

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *