Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Merge Two Arrays without Duplicates (No libraries allowed)

Tags:

java

arrays

Need assistance with programming issue.

Must be in Java. Cannot use any libraries (Arraylist, etc.).

int[] a = {1, 2, 3, 4, 8, 5, 7, 9, 6, 0}
int[] b = {0, 2, 11, 12, 5, 6, 8}

Have to create an object referencing these two arrays in a method that merges them together, removes duplicates, and sorts them.

Here's my sort so far. Having difficulty combining two arrays and removing duplicates though.

int lastPos;
int index;
int temp;

for(lastPos = a.length - 1; lastPos >= 0; lastPos--) {
    for(index = 0; index <= lastPos - 1; index++) {
        if(a[index] > a[index+1]) {
            temp = a[index];
            a[index] = a[index+1];
            a[index+1] = temp;
        }
    }
}
like image 884
BDelta Avatar asked Jan 07 '23 16:01

BDelta


2 Answers

a method that merges them together, removes duplicates, and sorts them.

I suggest you break this down into helper methods (and slightly tweak the order of operations). Step 1, merge the two arrays. Something like,

static int[] mergeArrays(int[] a, int[] b) {
    int[] c = new int[a.length + b.length];
    for (int i = 0; i < a.length; i++) {
        c[i] = a[i];
    }
    for (int i = 0; i < b.length; i++) {
        c[a.length + i] = b[i];
    }
    return c;
}

Step 2, sort the new array (your existing sort algorithm is fine). Like,

static void sortArray(int[] a) {
    for (int lastPos = a.length - 1; lastPos >= 0; lastPos--) {
        for (int index = 0; index <= lastPos - 1; index++) {
            if (a[index] > a[index + 1]) {
                int temp = a[index];
                a[index] = a[index + 1];
                a[index + 1] = temp;
            }
        }
    }
}

Finally, remove duplicates. Step 3a, count unique values. Assume they're unique, and decrement by counting adjacent (and equal) values. Like,

static int countUniqueValues(int[] c) {
    int unique = c.length;
    for (int i = 0; i < c.length; i++) {
        while (i + 1 < c.length && c[i] == c[i + 1]) {
            i++;
            unique--;
        }
    }
    return unique;
}

Then step 3b, take the unique count and build your result with the previous methods. Like,

public static int[] mergeDedupSort(int[] a, int[] b) {
    int[] c = mergeArrays(a, b);
    sortArray(c);
    int unique = countUniqueValues(c);
    int[] d = new int[unique];
    int p = 0;
    for (int i = 0; i < c.length; i++) {
        d[p++] = c[i];
        while (i + 1 < c.length && c[i] == c[i + 1]) {
            i++;
        }
    }
    return d;
}

Then you can test it with your arrays like

public static void main(String[] args) {
    int[] a = { 1, 2, 3, 4, 8, 5, 7, 9, 6, 0 };
    int[] b = { 0, 2, 11, 12, 5, 6, 8 };
    int[] c = mergeDedupSort(a, b);
    System.out.println(Arrays.toString(c));
}

And I get

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12]
like image 95
Elliott Frisch Avatar answered Jan 31 '23 05:01

Elliott Frisch


Merge Two Arrays without Duplicates and Sort it (No libraries used). Using an object.

public class MergeRemoveDupSort {    

public int[] mergeRemoveDupSortIt(int[] a, int[] b) {   
    int [] c = mergeIt(a,b);
    int [] d = removeIt(c);
    int [] e = sortIt(d);
    return e;
}

private int[] mergeIt(int[] a, int[] b) {   
    int[] c = new int[a.length + b.length];        
    int k=0;
    for (int n : a) c[k++]=n;        
    for (int n : b) c[k++]=n;   
    return c;
}

private int[] removeIt(int[] c) {  
    int len=c.length;
    for (int i=0;i<len-1;i++) 
        for (int j=i+1;j<len;j++)
            if (c[i] == c[j]) {
                for (int k=j;k<len-1;k++)
                    c[k]=c[k+1];
                --len;
            } 
    int [] r = new int[len];
    for (int i=0;i<r.length;i++)
        r[i]=c[i];
    return r;
}

private int[] sortIt(int[] a) {   
    for(int index=0; index<a.length-1; index++)
       for(int i=index+1; i<a.length; i++)
           if(a[index] > a[i]){
               int temp = a[index];
               a[index] = a[i];
               a[i] = temp;
           }
     return a;
}    

public void printIt(int[] a) {   
    System.out.print("[");
    for (int i=0;i<a.length;i++){
        System.out.print(a[i]);
        if (i!=a.length-1) System.out.print(",");
        else System.out.print("]");            
    }        
}

public static void main(String[] args) {
    int[] a = {1, 2, 3, 4, 8, 5, 7, 9, 6, 0};
    int[] b = {0, 2, 11, 12, 5, 6, 8};

    MergeRemoveDupSort array = new MergeRemoveDupSort();
    int [] r = array.mergeRemoveDupSortIt(a, b);
    array.printIt(r);        
}        
}
like image 31
SkyMaster Avatar answered Jan 31 '23 06:01

SkyMaster