-1

I am trying to merge two arrays, remove duplicates and arrange in ascending order. Whenever I am trying to do this it's showing wrong.

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
            
            
class Main {
            
    public static void main(String[] args) {
        int a[] = { 1, 2, 3, 4 };
        int b[] = { 1, 2, 3, 4, 5 };
                        
        HashMap<Integer, Integer> h1 = new HashMap<>();
                        
        for (int i = 0; i < a.length; i++)
            h1.put(a[i], i);
                        
            for (int i = 0; i < b.length; i++)
                h1.put(b[i], i);
                        
                for (Map.Entry<Integer, Integer> m1 : h1.entrySet())
                    System.out.print(m1.getKey() + " ");
                    System.out.println();
                }
            }
        }
    }
}
Dan
  • 3,647
  • 5
  • 20
  • 26
  • 1
    To merge an array see [How can I concatenate two arrays in Java?](https://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java) to sort an array see [Sort an array in Java](https://stackoverflow.com/questions/8938235/sort-an-array-in-java) As for checking duplicates, that can be done with a for loop. Try making a solution without a Hashmap and update your code here showing what you tried if you get stuck. – sorifiend Jun 22 '22 at 06:20
  • Just use a `TreeSet`. – tgdavies Jun 22 '22 at 06:30
  • Arrays that are need to be merged into a sorted array are already *sorted*, like in your data sample? Or the order of their elements is not specified? – Alexander Ivanchenko Jun 22 '22 at 11:32

3 Answers3

3

You can use TreeSet. It automatically removes duplicates and sort itself in ascending order:

int[] a = { 1, 2, 3, 4 };
int[] b = { 1, 2, 3, 4, 5 };

TreeSet<Integer> ts = new TreeSet<>();

for(int j : a) {
    ts.add(j);
}
for(int j : b) {
    ts.add(j);
}

System.out.println(ts);

The output will be: [1, 2, 3, 4, 5]

You can read more about TreeSets here.

0

First try to fix the curly brackets (they should match). The code should work but it can be optimized by using a HashSet instead of a HashMap

exitialium
  • 23
  • 2
0

The solution mentioned in comments with TreeSets is ideal, but if you wanted to do it the old-school way, see below for a rough working idea, although there is a lot that could be improved upon:

public static void main(String[] args) throws IOException, InterruptedException
{
    int a[] = { 1, 3, 7, 4, 2 };
    int b[] = { 1, 2, 3, 4, 5 };

    //Add all items to the array using a helper method
    //A better solution is shown here: https://stackoverflow.com/questions/8938235/sort-an-array-in-java
    for (int i : b){
        a = addToArray(a, i);
    }

    //Remove duplicates using a helper method
    //Ideally you would check duplicates before adding them to the array
    a = removeDuplicates(a);
    
    //Sort in ascending order
    Arrays.sort(a);
    
    System.out.println(Arrays.toString(a));
}

//Add one item to an array at a time
public static int[] addToArray(int[] array, int newItem){
    //create a new array that is 1 larger
    int[] newArray = new int[array.length + 1];
    //copy the array
    System.arraycopy(array, 0, newArray, 0, array.length);
    //Add the new element to the end
    newArray[array.length] = newItem;
    //return the new array
    return newArray;
}

public static int[] removeDuplicates(int[] array){
    int[] newArray = new int[0];
    
    //Iterate the array and check for duplicates
    for (int i : array){
        //Use a flag to track duplicates
        boolean duplicate = false;
        for (int j : newArray){
            if(i == j){
                //break when a duplicate is found and set the flag
                duplicate = true;
                break;
            }
        }

        //Only add the item to the new array if it's not a duplicate
        if(duplicate == false){
            newArray = addToArray(newArray, i);
        }
    }

    //Return the new array
    return newArray;
}

And the result of the main method output is:

[1, 2, 3, 4, 5, 7]
sorifiend
  • 5,927
  • 1
  • 28
  • 45