Created
May 22, 2019 10:36
-
-
Save kamaubrian/2c7a64158792e0b5920cf131c4c99b44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.cellulant.test; | |
import java.util.*; | |
public class Main { | |
public static String[] uniqueNames(String[] names1, String[] names2) { | |
ArrayList<String> firstNameList = new ArrayList<>(); | |
ArrayList<String> secondNameList = new ArrayList<>(); | |
firstNameList.addAll(Arrays.asList(names1)); | |
secondNameList.addAll(Arrays.asList(names2)); | |
Set<String> set = new LinkedHashSet<>(firstNameList); | |
set.addAll(secondNameList); | |
List<String> combinedNamesList = new ArrayList<>(set); | |
List<String> listTwoCopy = new ArrayList<>(secondNameList); | |
listTwoCopy.removeAll(firstNameList); | |
firstNameList.addAll(listTwoCopy); | |
String[] parsedArray = new String[firstNameList.size()]; | |
parsedArray = firstNameList.toArray(parsedArray); | |
return parsedArray; | |
} | |
public static void main(String[] args) { | |
String[] names1 = new String[]{"Ava", "Emma", "Olivia"}; | |
String[] names2 = new String[]{"Olivia", "Sophia", "Emma"}; | |
System.out.println(String.join(", ", Main.uniqueNames(names1, names2))); // should print Ava, Emma, Olivia, Sophia | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment