HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Merge and sorting arrays of String and int in more efficient methods

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sortingarraysmergemoreefficientmethodsandintstring

Problem

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 ``.

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 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 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]+" ");
        }
    }
}

Solution

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

public static int[] mergeAndSortIntArrays(int[] firstInt, int[] secondInt){
    List 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[]
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.

Code Snippets

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();
}
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;
}
System.out.println(Arrays.toString(mergeArrays(first, second)));

Context

StackExchange Code Review Q#129247, answer score: 2

Revisions (0)

No revisions yet.