1

I was able to get the two arrays to sort and merge but I can not figure out how to remove the duplicates. Can someone please help me with this? Here is my code so far:

public class FinalArray {
    public static void main(String[] args) {
        String[] testArray1 = {"coffee", "tea", "water"};
        String[] testArray2 = {"lemonade", "juice", "water"};
        mergeUniqueValues(testArray1, testArray2);
    }

    public static void mergeUniqueValues(String[] arr1, String[] arr2) {
        String[] arr3 = new String[arr1.length + arr2.length];

        for (int i = 0; i < arr1.length; i++) {
            arr3[i] = arr1[i];
        }
        for (int i = arr1.length, index = 0; i < arr1.length + arr2.length; i++, index++) {
            arr3[i] = arr2[index];
        }
        Arrays.sort(arr3);

        System.out.println("Your sorted array is: ");
        for (String str : arr3) {
            System.out.println(str);
        }
    }
}
Kate Macleod
  • 11
  • 1
  • 3

4 Answers4

3

Property of Set: It does not allow duplicates.

You can simply convert the array to Set (to avoid duplicates) and then convert it back to an array.

Here is a sample code for your reference:

public class Main {

    public static void main(String[] args) {

        String[] testArray1 = {"coffee", "tea", "water"};
        String[] testArray2 = {"lemonade", "juice", "water"};
        mergeUniqueValues(testArray1, testArray2);
    }

    public static void mergeUniqueValues(String[] arr1, String[] arr2) {
        Set noDuplicateSet = new HashSet();
        noDuplicateSet.addAll(Arrays.asList(arr1));
        noDuplicateSet.addAll(Arrays.asList(arr2));

        String[] noDuplicateArray = new String[noDuplicateSet.size()];
        noDuplicateSet.toArray(noDuplicateArray);

        Arrays.sort(noDuplicateArray);

        System.out.println("Your sorted array is: ");
        for (String str : noDuplicateArray) {
            System.out.println(str);
        }
    }
}

Output:

Your sorted array is: 
coffee
juice
lemonade
tea
water
miiiii
  • 1,580
  • 1
  • 16
  • 29
2

You can use Java Streams and distinct().

String[] result =
  Stream.concat( // combine
    Stream.of(array1),
    Stream.of(array1))
  .distinct()    // filter duplicates
  .sorted()      // sort
  .toArray(String[]::new);
daniu
  • 14,137
  • 4
  • 32
  • 53
0

You can take advantage from the java.util.TreeSet class, which is a collection which implements java.util.Set, and made of all unique values ordered by the natural order of the given elements. In your case, String does implement Comparable, so it can be naturally ordered when the element are inserted in the collection.

Here is the code you can test:

import java.util.*;

public class MergeArray {

    public static void main(String[] args) {

        String[] testArray1 = { "coffee", "tea", "water" };
        String[] testArray2 = { "lemonade", "juice", "water" };
        
        mergeUniqueValues(testArray1, testArray2);
    }

    public static void mergeUniqueValues(String[] arr1, String[] arr2) {
        
        Set<String> mergedList = new TreeSet(Arrays.asList(arr1));
        mergedList.addAll(Arrays.asList(arr2));
        String[] arr3 = mergedList.toArray(String[]::new);
        
        System.out.println("Your sorted array is: ");
        for (String str : arr3) {
            System.out.println(str);
        }
    }

}

And here is the output:

Your sorted array is: 
coffee
juice
lemonade
tea
water
Marco Tizzano
  • 1,726
  • 1
  • 11
  • 18
0

You can use Java Stream:

String[] testArray1 = {"coffee", "tea", "water"};
String[] testArray2 = {"lemonade", "juice", "water"};

String[] testArray3 = Stream.of(testArray1, testArray2)
        .flatMap(Arrays::stream).distinct().sorted().toArray(String[]::new);

Arrays.stream(testArray3).forEach(System.out::println);
//coffee
//juice
//lemonade
//tea
//water