return unique values array from two arrays java

Solutions on MaxInterview for return unique values array from two arrays java by the best coders in the world

showing results for - "return unique values array from two arrays java"
Julia
16 Sep 2016
1import java.util.Arrays;
2import java.util.List;
3import java.util.stream.Collectors;
4import java.util.stream.Stream;
5public class MergeNames {
6
7    public static String[] uniqueNames(String[] names1, String[] names2) {
8
9        String[] combine = Stream.concat(Arrays.stream(names1), Arrays.stream(names2))
10                .toArray(String[]::new);
11
12        List<String> distinctElements = Arrays.stream(combine)
13                .distinct()
14                .collect(Collectors.toList());
15
16        return distinctElements.toArray(new String[distinctElements.size()]);
17
18    }
19
20    public static void main(String[] args) {
21        String[] names1 = new String[] {"Ava", "Emma", "Olivia"};
22        String[] names2 = new String[] {"Olivia", "Sophia", "Emma"};
23        System.out.println(String.join(", ", MergeNames.uniqueNames(names1, names2))); // should print Ava, Emma, Olivia, Sophia
24    }
25}