# Thread: Searching and comparing Array elements

1. Member
Join Date
Nov 2009
Posts
2
Rep Power
0

## Searching and comparing Array elements

Can anyone point me to a tutorial on arrays.

I have 2 int arrays and I am trying to merge them together , remove duplicates and put them into a new separate array, and then sort the merged array.

Ex.

Array1[]= {1,2,3,4,7,8}
Array2[]={1,5,6,7}

// I need to merge these 2 arrays, sort, and remove duplicates and put in their own array.

Resulting in something like this:

sortedArray[]={1,2,3,4,5,6,7,8}
DuplicateArray[]={1,7}

2. What have you tried?

3. Member
Join Date
Feb 2011
Location
Posts
36
Rep Power
0

## reply remove duplicates in array

you can try the following ..

import java.util.Arrays;
/**
*
* @author hmetalia
*/
public class temparray
{
public static void main(String a[])
{
int[] Array1= {1,2,3,4,7,8};
int[] Array2={1,5,6,7};
mergeArray(Array1,Array2);
}

public static void mergeArray(int Array[],int[] Array1)
{
int [] SortedArray=new int[Array.length +Array1.length];
System.arraycopy(Array, 0, SortedArray, 0, Array.length);
System.arraycopy(Array1, 0, SortedArray, Array.length, Array1.length);
Arrays.sort(SortedArray);
int duplicates=0;
for(int i=1; i<SortedArray.length; i++) //might be size not length, I always forget.....
{
if(SortedArray[i]==SortedArray[i-1]) //from memory java arrays, like c's start from 0, if not change the 1 to a 2 in the loop above
{
duplicates ++;
}
}
int[] SortedArray1=new int[SortedArray.length-duplicates];
int[] duplicateArray=new int[duplicates];
int j=0;
int new_count=0;
SortedArray1[0]=SortedArray[0];
for(int i=1; i<SortedArray.length; i++) //again modify if arrays start at 1
{
if(SortedArray[i]!=SortedArray1[new_count])
{
SortedArray1[new_count+1]=SortedArray[i];
new_count++;
}
else
duplicateArray[j++]=SortedArray[i];
}
System.out.println("sorted array is ");
for(int i=0;i<SortedArray1.length;i++)
System.out.print(SortedArray1[i]);
System.out.print("\nduplicate array is ");
for(int i=0;i<duplicateArray.length;i++)
System.out.print(duplicateArray[i]);

}
}

4. Spoonfeeding a complete answer is only helping them cheat. Will you sit their exam for them as well?

5. Member
Join Date
Feb 2011
Location
Posts
36
Rep Power
0
sorry next time i will give hint only...

6. Hemant,

Fubarable have already asked you to use code tags in your previous post. But you are still not getting it. Use Code Tags next time around.

Then, spoon-feeding is not the culture here on this forum. You are considerably new to forum and hence a bit excited to help people. But don't serve the OP with a full fledged solution. Because that leaves them learning nothing.

Junky or someone else could have provided much better code in the first attempt itself. But we don't follow that practice here. Let the OP interact and think over various ways to resolve their problems. That way we actually help them learn something.

Hint them, guide them or show them the path. But don't hold their hand and get them to their destination. Let them travel their own journey. :)

I hope you are enough clear with this,

Goldest