Java > AP-1 > mergeTwo (CodingBat Solution)

Problem:

Start with two arrays of strings, A and B, each with its elements in alphabetical order and without duplicates. Return a new array containing the first N elements from the two arrays. The result array should be in alphabetical order and without duplicates. A and B will both have a length which is N or more. The best "linear" solution makes a single pass over A and B, taking advantage of the fact that they are in alphabetical order, copying elements directly to the new array.

mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3) → {"a", "b", "c"}
mergeTwo({"a", "c", "z"}, {"c", "f", "z"}, 3) → {"a", "c", "f"}
mergeTwo({"f", "g", "z"}, {"c", "f", "g"}, 3) → {"c", "f", "g"}


Solution:

public String[] mergeTwo(String[] a, String[] b, int n) {
  String out[] = new String[n];
  int aindex =0, bindex=0;
  for(int i=0; i<n; i++)
  {
    int cmp = a[aindex].compareTo( b[bindex] );
    if(cmp<=0)
    {
      out[i] = a[aindex++];
      if(cmp == 0) bindex++;
    }
    else
    {
      out[i] = b[bindex++];
    }
  } 
  return out;
}


19 comments :

  1. public String[] mergeTwo(String[] a, String[] b, int n) {
    String[] str=new String[n];
    int count=0;
    int acount=0;
    int bcount=0;
    while(count0){
    str[count]=b[bcount];
    count++;
    bcount++;
    }
    else if(x<0){
    str[count]=a[acount];
    count++;
    acount++;
    }
    else if(x==0){
    str[count]=a[acount];
    acount++;
    bcount++;
    count++;
    }
    }
    return str;
    }

    ReplyDelete
    Replies
    1. there is no description for x..

      Delete
  2. public String[] mergeTwo(String[] a, String[] b, int n) {
    int pos =0, val =0, i =0, j =0;
    String[] array = new String[n];
    while (pos < array.length){
    val = a[i].compareTo(b[j]);
    if (val == 0){
    array[pos++] = a[i];
    i++;
    j++;
    }else if (val < 0){
    array[pos++] = a[i];
    i++;
    }else if (val > 0){
    array[pos++] = b[j];
    j++;
    }
    }
    return array;
    }

    ReplyDelete
  3. public Strin[] mergeTwo(String[] a,String[] b,int n)
    {
    HashSet set=new HashSet;
    for(int i=0;i<a.length;i++)
    set.add(a[i]);
    for(int i=0;i<b.length;i++)
    set.add(b[i]);
    String str[]=set.toArray(new String[set.size()]);
    String strArr[]=new String[n];
    for(int i=0;i<n;i++)
    strArr[i]=str[i];
    return strArr;
    }

    ReplyDelete
  4. public Strin[] mergeTwo(String[] a,String[] b,int n)
    {
    HashSet set=new HashSet;
    for(int i=0;i<a.length;i++)
    set.add(a[i]);
    for(int i=0;i<b.length;i++)
    set.add(b[i]);
    String str[]=set.toArray(new String[set.size()]);
    String strArr[]=new String[n];
    for(int i=0;i<n;i++)
    strArr[i]=str[i];
    return strArr;
    }

    ReplyDelete
  5. public Strin[] mergeTwo(String[] a,String[] b,int n)
    {
    HashSet set=new HashSet;
    for(int i=0;i<a.length;i++)
    set.add(a[i]);
    for(int i=0;i<b.length;i++)
    set.add(b[i]);
    String str[]=set.toArray(new String[set.size()]);
    String strArr[]=new String[n];
    for(int i=0;i<n;i++)
    strArr[i]=str[i];
    return strArr;
    }

    ReplyDelete
  6. i didn't readthath A and B has the same length. My solution in that case...
    public String[] mergeTwo(String[] a, String[] b, int n) {

    String [] x =new String[n];
    String [] y = new String [a.length +b.length];

    for (int i=0; i<a.length; i++) {
    y[i] = a[i];

    }

    for (int i=0; i<b.length; i++) {
    y[a.length +i] = b[i];

    }

    Arrays.sort(y);
    for (int q=0; q<y.length-1; q++) {
    if(y[q].equals(y[q+1])) {
    y[q]="zzzzzzz";
    }
    }
    Arrays.sort(y);

    for(int w=0; w<n; w++) {
    x[w]=y[w];
    }
    return x;

    ReplyDelete

  7. public String[] mergeTwo(String[] a, String[] b, int n)
    {
    ArrayList merge = new ArrayList();
    String[] result = new String[n];
    for(int s = 0; s < a.length; s++)
    {
    merge.add(a[s]);
    }
    for(int x = 0; x < b.length; x++)
    {
    merge.add(b[x]);
    }
    Collections.sort(merge);
    for(int r = 0; r < merge.size() - 1; r++)
    {
    if(merge.get(r).equals(merge.get(r + 1)))
    {
    merge.remove(r);
    }
    }
    for(int i = 0; i < n; i++)
    {
    result[i] = merge.get(i);
    }
    return result;
    }

    ReplyDelete
    Replies
    1. ArrayList with triangle brackets are removed for some reason.

      Delete
    2. public String[] mergeTwo(String[] a, String[] b, int n)
      {
      ArrayList merge = new ArrayList();
      String[] result = new String[n];
      for(int s = 0; s < a.length; s++)
      {
      merge.add(a[s]);
      }
      for(int x = 0; x < b.length; x++)
      {
      merge.add(b[x]);
      }
      Collections.sort(merge);
      for(int r = 0; r < merge.size() - 1; r++)
      {
      if(merge.get(r).equals(merge.get(r + 1)))
      {
      merge.remove(r);
      }
      }
      for(int i = 0; i < n; i++)
      {
      result[i] = merge.get(i);
      }
      return result;
      }

      Delete
  8. public String[] mergeTwo(String[] a, String[] b, int n) {
    String[] res = Arrays.copyOfRange(a, 0, n);
    for(int i = 0; i < b.length; i++){
    for(int y = 0 ; y < res.length; y++){
    if(y > 0 && b[i].compareTo(res[y-1]) == 0) break;
    if(b[i].compareTo(res[y]) < 0){
    String repl = res[y];
    res[y] = b[i];
    if(y+1 < res.length) res[y+1] = repl;
    break;
    }
    }
    }
    return res;
    }


    ReplyDelete
  9. public String[] mergeTwo(String[] a, String[] b, int n) {
    List list = new ArrayList();
    for(int i = 0;i<n;i++){
    if(!list.contains(a[i]))list.add(a[i]);
    if(!list.contains(b[i]))list.add(b[i]);
    }
    Collections.sort(list);
    return list.subList(0,n).toArray(new String[n]);
    }

    ReplyDelete
  10. public String[] mergeTwo(String[] a, String[] b, int n) {
    String[] arr = new String[n];

    for (int i = 0, j = 0, arrPlace = 0 ; arrPlace < arr.length; ) {
    char one = (char) Math.min(a[i].charAt(0), b[j].charAt(0));
    arr[arrPlace] = "" + one;
    arrPlace++;
    if (a[i].charAt(0) < b[j].charAt(0)) i++;
    else if (a[i].charAt(0) > b[j].charAt(0)) j++;
    else {
    i++;
    j++;
    }
    }
    return arr;
    }

    ReplyDelete
  11. Linear solution using the concept of Merge Sort:

    public int commonTwo(String[] a, String[] b) {
    int count=0;
    int i=0,j=0;
    Set set = new HashSet();
    while( i< a.length && j < b.length){
    int cmp = a[i].compareTo(b[j]);
    if(cmp == 0){
    if(!set.contains(a[i])){
    set.add(a[i]);
    count++;
    }
    i++;
    j++;
    }
    else if(cmp < 0)
    i++;
    else
    j++;
    }
    return count;
    }

    ReplyDelete
  12. public String[] mergeTwo(String[] a, String[] b, int n) {
    String[] out = new String[n];
    int aIndex = 0, bIndex = 0;
    for(int i = 0; i < n; i++) {
    if(a[aIndex].compareTo( b[bIndex] ) <= 0) {
    out[i] = a[aIndex++];
    if(out[i].compareTo( b[bIndex]) == 0) {
    bIndex++;
    }
    } else {
    out[i] = b[bIndex++];
    }
    }
    return out;
    }

    ReplyDelete
  13. public String[] mergeTwo(String[] a, String[] b, int n) {
    String[] copy = Arrays.copyOfRange(a, 0, n);
    for (final String s : b) {
    int j = 0;
    while (j < copy.length) {
    if (j > 0 && s.compareTo(copy[j - 1]) == 0) {
    break;
    }
    if (s.compareTo(copy[j]) < 0) {
    String result = copy[j];
    copy[j] = s;
    if (j + 1 < copy.length) {
    copy[j + 1] = result;
    break;
    }
    }
    j++;
    }
    }
    return copy;
    }

    ReplyDelete
  14. public String[] mergeTwo(String[] a, String[] b, int n) {
    List list = new ArrayList<>();
    for (int i = 0; i < n; i++) {
    if (!list.contains(a[i])) list.add(a[i]);
    if (!list.contains(b[i])) list.add(b[i]);
    }
    Collections.sort(list);
    return list.subList(0,n).toArray(new String[n]);
    }

    ReplyDelete
  15. public String[] mergeTwo(String[] a, String[] b, int n) {
    TreeSet myTreeSet = new TreeSet();
    Collections.addAll(myTreeSet,a);
    Collections.addAll(myTreeSet,b);
    return Arrays.copyOfRange(myTreeSet.toArray(new String[n]), 0, n);
    }

    ReplyDelete
  16. public String[] mergeTwo(String[] a, String[] b, int n) {
    return java.util.stream.Stream.concat(Arrays.stream(a),Arrays.stream(b)).distinct().sorted().limit(n).toArray(String[]::new);
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact