Introduction

In this tutorial we will show you how to merge string values from two arrays and produce the final array with unique string values. We 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

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

We have implemented two ways to return the array with unique elements.

package com.roytuts.merge.two.arrays.unique.values;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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 = mergeArrays(arr1, arr2);
		System.out.println(Arrays.toString(arr));

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

	public static String[] mergeArrays(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[] mergeArrays2(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 the Program

Executing the above code will give you below output:

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

Download

Thanks for reading.

Tags:

Leave a Reply

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