Answers for "unique elements from two arrays in java"

1

return unique values array from two arrays java

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class MergeNames {

    public static String[] uniqueNames(String[] names1, String[] names2) {

        String[] combine = Stream.concat(Arrays.stream(names1), Arrays.stream(names2))
                .toArray(String[]::new);

        List<String> distinctElements = Arrays.stream(combine)
                .distinct()
                .collect(Collectors.toList());

        return distinctElements.toArray(new String[distinctElements.size()]);

    }

    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(", ", MergeNames.uniqueNames(names1, names2))); // should print Ava, Emma, Olivia, Sophia
    }
}
Posted by: Guest on October-03-2020
0

unique elements in array java

import java.util.ArrayList;

public class UniqueNumbersInArray {

    public static void main(String[] args) {


    int arr[] = { 4,5,5,4,5,6,5,8,4,7};

        ArrayList<Integer> uniqueArr = new ArrayList<Integer>();
        for (int i = 0; i < arr.length; i++) {

            int k = 0;
            if (!uniqueArr.contains(arr[i])){
                uniqueArr.add(arr[i]);
                k++;

                for (int j = i; j < args.length ; j++) {
                    if (arr[i]==arr[j]){
                        k++;
                    }
                }

                System.out.println(arr[i]);
                System.out.println(k);
            }

        }
    }
}
Posted by: Guest on January-14-2021

Code answers related to "unique elements from two arrays in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language