Results 1 to 6 of 6
- 02-15-2011, 02:57 AM #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}
- 02-15-2011, 03:09 AM #2
What have you tried?
- 02-15-2011, 05:26 AM #3
Member
- Join Date
- Feb 2011
- Location
- Ahmedabad
- 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]);
}
}
- 02-15-2011, 05:44 AM #4
Spoonfeeding a complete answer is only helping them cheat. Will you sit their exam for them as well?
- 02-15-2011, 05:58 AM #5
Member
- Join Date
- Feb 2011
- Location
- Ahmedabad
- Posts
- 36
- Rep Power
- 0
sorry next time i will give hint only...
- 02-15-2011, 06:06 AM #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,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
Similar Threads
-
Trouble comparing Elements while using Generics
By ryuzog in forum New To JavaReplies: 13Last Post: 12-16-2010, 09:26 AM -
comparing elements in array
By garyscott101 in forum New To JavaReplies: 14Last Post: 12-10-2008, 03:01 PM -
Searching In a String Array - Problem
By DillMan in forum New To JavaReplies: 4Last Post: 12-07-2008, 09:12 PM -
comparing array elements
By Jeremy720 in forum New To JavaReplies: 2Last Post: 10-13-2008, 02:33 AM -
comparing array using character
By Anseki in forum New To JavaReplies: 7Last Post: 10-03-2008, 07:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks