-1

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?

RShome
  • 489
  • 2
  • 11
  • 35
  • 2
    Go on then: try! Have a go, and we would be delighted to help you with specific problems. – Andy Turner Apr 18 '19 at 15:37
  • 1
    This smells like homework, but if I actually had to do this in Java, array is probably not the collection type I would use. – Tim Biegeleisen Apr 18 '19 at 15:37
  • It's really not homework I promise. I am trying coding challenges. And I don't even know how to start. – RShome Apr 18 '19 at 15:38
  • 1
    Yeah. Any time you need to do manipulations of collections of data, arrays would not be the first choice when you have a .retainAll() method already in the Java collections api –  Apr 18 '19 at 15:38
  • 1
    @RShome try to find out what data structures can be used to hold unique values. – Andy Turner Apr 18 '19 at 15:39
  • or could someone point me to where I could learn about this? – RShome Apr 18 '19 at 15:39
  • 2
    You basically want to create a set. https://docs.oracle.com/javase/7/docs/api/java/util/Set.html – junvar Apr 18 '19 at 15:39
  • 1
    @junvar not that it counts in this case, but java 7 is pretty much obsolete by now. You should link to more modern documentation. – Federico klez Culloca Apr 18 '19 at 15:41
  • Folks I added a solution of mine which seems to work in my IDE. However the coding challenge requests use of the separate function. Why is that needed? Then I would need to pass back the joint array as a pararmeter in the Main() method which is not allowed. – RShome Apr 19 '19 at 16:14

5 Answers5

4

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()]);
    }
}
Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
1
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)));
    }
}
Xavier Lambros
  • 776
  • 11
  • 19
Turkmen
  • 11
  • 1
1
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()]);
}
meren
  • 432
  • 5
  • 19
1
 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]);
    }
Ikbel
  • 1,817
  • 1
  • 17
  • 30
0

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 !

Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
Conffusion
  • 4,335
  • 2
  • 16
  • 28