0

I need to create a program that not only put together 2 arrays, but to also avoid printing twice a number that is repeated on the on the arrays.

For example:

1,2,3,4,5 //Array 1

5,6,7,8,4 //Array 2

1,2,3,4,5,6,7,8 //Array 1 & 2 together

I heard that the Hashshet can help out to perform this, but I'm not really sure it works. I just began programming, so i don't know a lot of this stuff.

Hope someone can help me out.

Thanks

Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25
E B
  • 5
  • 2
  • 5
  • 2
    You are on the right path, just code it! – Juned Ahsan Oct 08 '13 at 05:39
  • so it depends what you like to do exactly. Do you want to eliminate duplicates, do you want to order them, ... The classes Arrays, Collections and the Interface Collection are good starting points – boskop Oct 08 '13 at 05:40
  • Take a look at these links: http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java and http://stackoverflow.com/questions/4697255/combine-two-integer-arrays – Pradip Oct 08 '13 at 05:49
  • This look like a duplicated question see that link http://stackoverflow.com/questions/5818057/union-of-2-arrays-in-java – Jans Oct 08 '13 at 05:50

9 Answers9

1
HashSet<Integer> hs = new HashSet<Integer>();

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

In the same way do for second array. hs will not contain any duplicate value.

RiaD
  • 46,822
  • 11
  • 79
  • 123
Ankit Jain
  • 2,230
  • 1
  • 18
  • 25
1

Use following simple code:

List<Integer> arr1=new ArrayList<>();   //1 2 3 4 5
List<Integer> arr2=new ArrayList<>();   // 5 6 7 8
Set<Integer> res=new HashSet<>();
res.addAll(arr1);
res.addAll(arr2);
System.out.println(res); //1 2 3 4 5 6 7 8
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

step 1 : initialize third new array

step 2 : iterate both array and store all elements in third array

step 3 : sort third array

0

Combine

    int lengthA = arrayA.length;
    int lengthB = arrayB.length;
    Sample[] result = new Sample[arrayA + arrayB];
    System.arraycopy(arrayA, 0, result, 0, lengthA);
    System.arraycopy(arrayA, 0, result, lengthA, lengthB);

And then add all the array value into the Set. It will reduce the duplicate values.

newuser
  • 8,338
  • 2
  • 25
  • 33
0

I'm going to give you the general algorithm and I'll leave it to you to code it.

  1. Create a new array (called it result) with size equal to array1.length+array2.length
  2. Create a hashmap of type <int, boolean>
  3. While iterating through your array, before copying numbers into "result" check to see if the number exists in the hashmap (if the boolean is true it exists). If it doesn't you're safe to copy into result, flip the boolean at that number to true.
  4. When you get to the end of your first array. Set the index variable of your second loop equal to the length of the first array and repeat step three.

In case you don't know HashMap this is how to initialize it. Get() will retrieve an element by its key, put() will put a value at the key you specify (in your case that will be the number).

<!-- language: java -->
HashMap<int,boolean> map = new HashMap<int,boolean>();
0

Add ArrayList1, ArrayList2 and produce a Single arraylist ArrayList3.

Now convert it into

Set Unique_set = new HashSet(Arraylist3);

in the unique set you will get the unique elements.

Note

ArrayList allows to duplicate values. Set doesn't allow the values to duplicate. Hope your problem solves.

0

You can use LinkedHashSet if you need to remove duplicates and preserve an order in which you insert items:

Set<Integer> set = new LinkedHashSet<Integer>();

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

// set now contains 1,2,3,4,5

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

// set now contains 1,2,3,4,5,6,7,8

If you don't need to preserve an insertion order, you can change set's implementaion to HashSet:

Set<Integer> set = new HashSet<Integer>();
aga
  • 27,954
  • 13
  • 86
  • 121
0

Try this:

    String arr[] = {"1", "2", "3", "4"};
    String arr2[] = {"1", "2", "3", "4", "5"};
    List<String> numList = new ArrayList<String>(Arrays.asList(arr));
    Collections.addAll(numList, arr2);
    Set<String> aSet = new HashSet<String>(numList);
    Iterator iterator = aSet.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
Jhanvi
  • 5,069
  • 8
  • 32
  • 41
-1

i hope if it will help you

merge sort algorithm

merge sort code

enter image description here

Mostafa Jamareh
  • 1,389
  • 4
  • 22
  • 54
  • 1
    This is only useful if he wants the array sorted (which I'm assuming he doesn't). Moreover it does not eliminate duplicates, it just makes them easier to find. –  Oct 08 '13 at 05:50
  • in merge sort you learn how to combine multiple arrays.and also it shows that he did something to sort them so he could use merge sort which do the same thing – Mostafa Jamareh Oct 08 '13 at 05:54
  • But you're not eliminating duplicates and you're corrupting the order of the arrays –  Oct 08 '13 at 05:54
  • he could check if there is duplicated item and go for the next item – Mostafa Jamareh Oct 08 '13 at 05:56
  • I realize that but it still doesn't maintain the order of the elements. You're sorting the array, he didn't say he needed it sorted. –  Oct 08 '13 at 05:59