'How to return an array that contains values, which appear in two arrays? And no duplicates

I want to pass two arrays of names so that 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 MergeNames.uniqueNames(new String[]{'Ava', 'Emma', 'Olivia'}, new String[]{'Olivia', 'Sophia', 'Emma'}) should return an array containing Ava, Emma, Olivia, and Sophia in any order.

Need to basically implement the uniqueNames method. Apologies for asking, I am new to Java programming and trying to become a developer by trying coding challenges.

public class MergeNames {

    public static String[] uniqueNames(String[] names1, String[] names2) {
        throw new UnsupportedOperationException("Waiting to be implemented.");      
    }

    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
    }
}

******MY SOLUTION ANY FEEDBACK WELCOME******

import java.util.*;

public class MergeNames {

public static void main(String[] args) {

    String[] names1 = new String[] {"Ava", "Emma", "Olivia"};
    String[] names2 = new String[] {"Olivia", "Sophia", "Emma"};

    Set<String> mySet1 = new HashSet<String>(Arrays.asList(names1));
    Set<String> mySet2 = new HashSet<String>(Arrays.asList(names2));

    Set<String> union = new HashSet<String>(mySet1);
    union.addAll(mySet2);
    System.out.println("Union of the two Sets with no duplicate names : " + union);

}
}

I'm not sure why the uniqueNames function is needed?



Solution 1:[1]

In comments you mentioned that you have to do this in a different method and give the joint array as parameter back to main(). Well by this statement I think they mean that you have to return the joint array back to main() "Not as a parameter"

You can go like this:

import java.util.*;

public class MergeNames 
{
    public static void main(String[] args) 
    {
        String[] names1 = new String[] {"Ava", "Emma", "Olivia"};
        String[] names2 = new String[] {"Olivia", "Sophia", "Emma"};
        String[] Names = mergeNames(names1, names2);
        for(String n: Names)
            System.out.print(" "+ n);
    }

    public static String[] mergeNames(String[] n1, String[] n2)
    {
        Set<String> mySet1 = new HashSet<String>(Arrays.asList(n1));
        Set<String> mySet2 = new HashSet<String>(Arrays.asList(n2));
        Set<String> union = new HashSet<String>(mySet1);
        union.addAll(mySet2);
        return  union.toArray(new String[union.size()]);
    }
}

Solution 2:[2]

import java.util.ArrayList;

public class tes1 {

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

        List<String> list = new ArrayList<String>();
        for (String name : names1) {
            list.add(name);
        }
        for (String name : names2) {
            if (!list.contains(name)) {
                list.add(name);
            }
        }
        String[] a = list.toArray(new String[list.size()]);
        return a;

    }

    public static void main(String[] args) {
        String[] names1 = new String[] { "Ava", "Emma", "Olivia" };
        String[] names2 = new String[] { "Olivia", "denene", "haloil", "Sophia", "Emma" };

        System.out.println(String.join(", ", tes1.uniqueNames(names1, names2)));
    }
}

Solution 3:[3]

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

  if(names1 == null || names2 == null){
    return null;
  }

  HashSet<String> set = new HashSet<>();
  for(String name : names1){
    set.add(name);
  }
  for(String name : names2){
    set.add(name);
  }
  System.out.println(set);
  return set.toArray(new String[set.size()]);
}

Solution 4:[4]

 public static String[] uniqueNames(String[] names1, String[] names2) {
        List<String> resArray = new ArrayList<>(Arrays.asList(names1));
        resArray.addAll(Arrays.asList(names2));
        return new HashSet<>(resArray).toArray(new String[0]);
    }

Solution 5:[5]

As @junvar suggested, look at java.util.Set

  • Create a new java.util.TreeSet<String> myset
  • Loop through each array and for every name value call myset.add(). Set will remove duplicates for you
  • To convert your set back to an array, see Converting Set<String> to String array

Good Luck !

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zain Arshad
Solution 2 Xavier Lambros
Solution 3 meren
Solution 4 Ikbel
Solution 5 Zain Arshad