Results 1 to 6 of 6
  1. #1
    jmanswrd is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default 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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    What have you tried?

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

    Default 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. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

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

  5. #5
    Join Date
    Feb 2011
    Location
    Ahmedabad
    Posts
    36
    Rep Power
    0

    Default

    sorry next time i will give hint only...

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    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
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Trouble comparing Elements while using Generics
    By ryuzog in forum New To Java
    Replies: 13
    Last Post: 12-16-2010, 09:26 AM
  2. comparing elements in array
    By garyscott101 in forum New To Java
    Replies: 14
    Last Post: 12-10-2008, 03:01 PM
  3. Searching In a String Array - Problem
    By DillMan in forum New To Java
    Replies: 4
    Last Post: 12-07-2008, 09:12 PM
  4. comparing array elements
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-13-2008, 02:33 AM
  5. comparing array using character
    By Anseki in forum New To Java
    Replies: 7
    Last Post: 10-03-2008, 07:28 PM

Tags for this Thread

Posting Permissions

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