3
\$\begingroup\$

Are there better methods for merge and sorting arrays of int and String in more efficient methods both time-complexity wise and code-pattern wise? I also wonder if I could have a generic method that could work for any kind of given type like <E>.

I am preparing for coding interviews, so I prefer to see solutions that involve known Java APIs and nothing fancy.

/**
 * Created by mona on 5/24/16.
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MergeArrays {

    public static String[] mergeAndSortStringArrays(String[] first, String[] second){
        List<String> merged = new ArrayList<>();
        Collections.addAll(merged,first);
        Collections.addAll(merged,second);
        Collections.sort(merged);
        return merged.toArray(new String[merged.size()]);
    }

    public static int[] mergeAndSortIntArrays(int[] firstInt, int[] secondInt){

        List<Integer> merged = new ArrayList<>();

        for (int i=0; i<firstInt.length; i++){
            merged.add(firstInt[i]);
        }

        for (int i=0; i<secondInt.length; i++){
            merged.add(secondInt[i]);
        }

        Collections.sort(merged);
        int[] result=new int[merged.size()];
        for (int i=0; i<merged.size(); i++){
            result[i]=merged.get(i);
        }
        return result;
    }


    public static void main(String[] args){
        String[] first={"hi", "mona", "how", "are", "you"};
        String[] second={"hello", "world", "good", "morning", "USA"};
        String[] merged=mergeAndSortStringArrays(first, second);
        for (int i=0; i<merged.length; i++){
            System.out.print(merged[i]+" ");
        }

        int[] firstInt={1, 4, 2, 6, 7};
        int[] secondInt={2, 8, 2, 9, 1, 12};
        int[] resultedMerge=mergeAndSortIntArrays(firstInt, secondInt);
        for (int i=0; i<resultedMerge.length; i++){
            System.out.print(resultedMerge[i]+" ");
        }
    }
}
\$\endgroup\$
2
\$\begingroup\$

mergeAndSortStringArrays looks good to me. What comes to mergeAndSortIntArrays, you can clean it a bit:

public static int[] mergeAndSortIntArrays(int[] firstInt, int[] secondInt){
    List<Integer> merged = new ArrayList<>();
    IntStream.of(firstInt) .forEach(merged::add);
    IntStream.of(secondInt).forEach(merged::add);
    Collections.sort(merged);
    return merged.stream().mapToInt((Integer i) -> (i)).toArray();
}

Generic implementation

Also, you could do

public static <E extends Comparable<? super E>> E[]
mergeArrays(final E[] firstArray, final E[] secondArray) {
    final E[] ret = Arrays.copyOf(firstArray, 
                                  firstArray.length + secondArray.length);
    System.arraycopy(secondArray, 
                     0, 
                     ret, 
                     firstArray.length, 
                     secondArray.length);

    Arrays.sort(ret);
    return ret;
}

This, however, will not accept an int[] as the argument type, yet it will gladly work on Integer[] and other object arrays.

A minor tip

Instead of writing for loops for printing arrays, you can always say:

System.out.println(Arrays.toString(mergeArrays(first, second)));

Hope that helps.

\$\endgroup\$
  • \$\begingroup\$ can both methods be combined into one? \$\endgroup\$ – Mona Jalal May 25 '16 at 8:00
  • \$\begingroup\$ Yes, they may be combined into one generic class method (see the latest edit), yet it won't accept int[] as an argument. \$\endgroup\$ – coderodde May 25 '16 at 8:28
0
\$\begingroup\$

Actually there is no need to box int. Also there is a nice companion class called Arrays.

public static <E extends Comparable<E>> E[] mergeAndSort(E[] e1, E[] e2) {
  E[] result = Arrays.copyOf(e1, e1.length + e2.length);
  System.arraycopy(e2, 0, result, e1.length, e2.length);
  Arrays.sort(result);
  return result;
}

public static int[] mergeAndSort(int[] i1, int[] i2) {
  int[] result = Arrays.copyOf(i1, i1.length + i2.length);
  System.arraycopy(i2, 0, result, i1.length, i2.length);
  Arrays.sort(result);
  return result;
}

Since String is an Object and not a primitive you can go with the more general solution.

If you've got a reasonable sized array you can also apply a Arrays::parallelSort but this is left as exercise.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.