Created
October 25, 2018 05:56
-
-
Save xola139/42c63df0a78ffbb455622d02305202a6 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
2. Common NamesJAVA ARRAYS PUBLIC NEW | |
Implement the findCommon method. When passed two arrays of names, it will return an array containing the names that appear | |
in either or both arrays. The returned array should have no duplicates. | |
For example, calling CommonNames.findCommon(new String[]{'Ava', 'Emma', 'Olivia'}, new String[]{'Olivia', 'Sophia', 'Emma'}) | |
should return an array containing Ava, Emma, Olivia, and Sophia in any order. | |
import java.util.Arrays; | |
public class CommonNames { | |
public static String[] findCommon(String[] names1, String[] names2) { | |
//throw new UnsupportedOperationException("Waiting to be implemented."); | |
String [] res = (String[]) Stream.concat(Stream.of(names1), Stream.of(names2)).distinct().toArray(b -> new String[b]); | |
return res; | |
} | |
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(", ", CommonNames.findCommon(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