1. Member
Join Date
Sep 2012
Posts
16
Rep Power
0

## Re: Array Merger

thats the same thing but my goal is to show the list of the merged arrays without showing the repeating numbers like:
1,2,3,4,4,5,6,7,8,9
it should only be;
1,2,3,4,5,6,7,8,9

2. ## Re: Array Merger

Sorry, it is not the same thing.
With the first there are three arrays used: two input, one merged & made unique
With the second there are four arrays used: two input, one merged and one with unique elements.

3. Member
Join Date
Sep 2012
Posts
16
Rep Power
0

## Re: Array Merger

ok id go with the first one...

4. ## Re: Array Merger

What is wrong with one of the Set impementations? (also see my reply#2); arrays are so Fortranesque and soooo clumsy for the job ...

kind regards,

Jos

5. Member
Join Date
Sep 2012
Posts
16
Rep Power
0

## Re: Array Merger

it is what my teacher wants us to do ..

6. ## Re: Array Merger

Again make the list of steps to go through the array and remove duplicates.
A way to visualize what needs to be done would be to write on a piece of paper a line of numbers with duplicates. Start at the left and move to the right. If the current number is already to the left of the current position, it's a duplicate and needs to be overwritten by copying a unique number over it from those numbers to the right. Continue to the end of the array.

When you get done there will be unused slots at the end of the array, one for each duplicate number.

7. Member
Join Date
Sep 2012
Posts
7
Rep Power
0

## Re: Array Merger

hey Yaki,
I should not write down the code for removing duplicates from the array, because you wiil not get to learn and will loose that joy when you ll finish it, so here are the steps I can suuges.
== take your merged array== iterate through it using for loops== get 1-1 element from that and compare it to all next to it== if it is equal then remove the another element with which you are comparing.
Now if you want to create fourth array of unique elements without disturbing third array:
iterate from third array== pick an int == iterate through fourth array == check if this int is there == if yes == break == if no add it in fourth array.

Hope this will help!

8. Member
Join Date
Sep 2012
Posts
16
Rep Power
0

## Re: Array Merger

Originally Posted by hardcoder
hey Yaki,
I should not write down the code for removing duplicates from the array, because you wiil not get to learn and will loose that joy when you ll finish it, so here are the steps I can suuges.
== take your merged array== iterate through it using for loops== get 1-1 element from that and compare it to all next to it== if it is equal then remove the another element with which you are comparing.
Now if you want to create fourth array of unique elements without disturbing third array:
iterate from third array== pick an int == iterate through fourth array == check if this int is there == if yes == break == if no add it in fourth array.

Hope this will help!

heres my code i still cant get it right!

Java Code:
```package app.runner;

import java.util.Scanner;
import java.util.TreeSet;

public class Runner {

public static void main(String[] args)  {
int firstArray[] = {1,1,1,1,1};
int secondArray[] = {6,7,8,9,10};
int result[]= new int[10];
int talagangResult[] = new int [10];
int x=0;
int r=0;

//this code is for the merging of two arrays..
for( x=0;x<firstArray.length;x++ ){

result[x] = firstArray[x];
}
for(int y=0;y<secondArray.length;y++ ){
result[y+5] = secondArray[y];
}

//this is the start of the code that will be incharge of
//putting the number on an array wthout the duplicates:::
for(r=0;r<result.length;r++ ){
boolean isMapupunta = false;
for(int fLoop = 0 ;fLoop<result.length ; fLoop++ ){

//this code tests if result[r] is equal to
//result[fLoop] this tests if a number is a duplicate of another number
// then it will set isMapupunta "true" and
//if it is set as true then
//it will not go on the second if statement
// the second if statement is
//putting the non duplicate numbers
// to array " talagangResult[x]
System.out.println("   result: "+ result[r]+ " fLoop: "+result[fLoop]);
if(result[r] == result[fLoop]){
isMapupunta =true;
break;
}

if(!isMapupunta){
talagangResult[x] = result[x];

}

}
}
//this here prints the numbers
//that is contained by talagangResult through the process

for(int s=0;s<talagangResult.length;s++ ){
System.out.println("result: "+talagangResult[s]);
}

}
}```

9. ## Re: Array Merger

Can you post the program's output so we can see what it does?

10. Member
Join Date
Sep 2012
Posts
16
Rep Power
0

## Re: Array Merger

this is the output :

this is for checking the loop:
result: 1 fLoop: 1
result: 1 fLoop: 1
result: 1 fLoop: 1
result: 1 fLoop: 1
result: 1 fLoop: 1
result: 6 fLoop: 1
result: 6 fLoop: 1
result: 6 fLoop: 1
result: 6 fLoop: 1
result: 6 fLoop: 1
result: 6 fLoop: 6
result: 7 fLoop: 1
result: 7 fLoop: 1
result: 7 fLoop: 1
result: 7 fLoop: 1
result: 7 fLoop: 1
result: 7 fLoop: 6
result: 7 fLoop: 7
result: 8 fLoop: 1
result: 8 fLoop: 1
result: 8 fLoop: 1
result: 8 fLoop: 1
result: 8 fLoop: 1
result: 8 fLoop: 6
result: 8 fLoop: 7
result: 8 fLoop: 8
result: 9 fLoop: 1
result: 9 fLoop: 1
result: 9 fLoop: 1
result: 9 fLoop: 1
result: 9 fLoop: 1
result: 9 fLoop: 6
result: 9 fLoop: 7
result: 9 fLoop: 8
result: 9 fLoop: 9
result: 10 fLoop: 1
result: 10 fLoop: 1
result: 10 fLoop: 1
result: 10 fLoop: 1
result: 10 fLoop: 1
result: 10 fLoop: 6
result: 10 fLoop: 7
result: 10 fLoop: 8
result: 10 fLoop: 9
result: 10 fLoop: 10

and this are the output that should be 1,6,7,8,9,10

result: 0
result: 0
result: 0
result: 0
result: 0
result: 6
result: 0
result: 0
result: 0
result: 0

11. ## Re: Array Merger

A better way to show the contents of an array is to used the Arrays class's toString() methd:
Java Code:
```        System.out.println("res="+java.util.Arrays.toString(result)); // res=[1, 1, 1, 1, 1, 6, 7, 8, 9, 10]

System.out.println("talres="+java.util.Arrays.toString(talagangResult)); //talres=[0, 0, 0, 0, 0, 6, 0, 0, 0, 0]```
Go back and do the logic by using a piece of paper to see how to advance the array pointers to make sure you advance each one at the correct time.
Also check when isMapupunta is true and when it will be set false as the code loops and the break is executed. Print out its value in the println that's there so you can see when tis true or false.
Last edited by Norm; 09-09-2012 at 03:24 PM.

12. ## Re: Array Merger

You need more debug print out to show what is happening. Print out the values of the from/source (result) and to/target()talagangResult) indexes for when the code copies an element and the vaue of the element that is being copied.

Page 2 of 2 First 12

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•