Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By gimbal2

Thread: AVOID RE-Printing of Duplicate Values in an Array!

  1. #1
    Join Date
    Feb 2014
    Posts
    15
    Rep Power
    0

    Default AVOID RE-Printing of Duplicate Values in an Array!

    Hello Java World
    Thanks, for reading this.

    I have a practice program (written with NetBeans IDE 7.4) that calls methods to:
    (1) fill an array with user input values
    (2) sort the array values into ascending order
    (3) print the array in assorted order.
    (4) print the array without any duplicates

    Method (4) prints the contents of the array without printing any duplicates. That is, if a number in the array has already been printed, it will not be printed again.

    Method (4) seems to work, but may be inefficient or I may have done too much work making it difficult on myself or making the logic too confusing.

    Is there a better way to do this WITHOUT using built-in functions in Java's library?
    (Such as writing this method using for loops and counter variables: As practice I am supposed to avoid using Deleting functions for duplicates)

    Here is my code:

    public static void Duplicates(int [] array){
    int duplicates = 1;
    String Output = "";
    for(int i = 0; i < array.length -1; i++) {
    if (array[i] != array[i+1]){
    duplicates ++;
    } // end of if
    } // end of i loop
    int [] tempArray = new int [duplicates];
    int j = 0;
    tempArray[0] = array[0];

    for(int i = 0; i < array.length - 1; i++) {
    if (array[i] != array[i+1]){
    j++;
    tempArray[j] = array[i+1];
    } // end of if
    } // end of i loop
    for (int i = 0; i < tempArray.length ; i++) {
    if(Output == ""){
    Output = "The array without duplicates is: " + "\n" + tempArray[i];
    }
    else {
    Output = Output + ", " + tempArray[i];
    }
    } // end of i loop
    System.out.println(Output);
    }


    Fingers crossed!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: AVOID RE-Printing of Duplicate Values in an Array!

    Assume the array is sorted and your program is about to print element #i (in a loop); it can only be a duplicate value if that element isn't the first element (i != 0) and it is not equal to the previous element.

    kind regards,

    Jos
    gimbal2 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,050
    Rep Power
    6

    Default Re: AVOID RE-Printing of Duplicate Values in an Array!

    Jebus its so simple. And I was racking my brain trying to come up with the easy solution (because there always is one). But...

    it can only be a duplicate value if that element isn't the first element (i != 0) and it is not equal to the previous element.
    I'd say it is a duplicate if it IS equal to the previous value.
    NoviceJavaNoob likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: AVOID RE-Printing of Duplicate Values in an Array!

    Quote Originally Posted by gimbal2 View Post
    Jebus its so simple. And I was racking my brain trying to come up with the easy solution (because there always is one). But...



    I'd say it is a duplicate if it IS equal to the previous value.
    Yep, I noticed that too after I posted my reply; I consider it one of my famous OBOEs (Off By One Errrors (tm)) in the boolean domain ... I've never been good with semantic double negations ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,050
    Rep Power
    6

    Default Re: AVOID RE-Printing of Duplicate Values in an Array!

    I have the same problem with dates; I always screw up when needing to validate dates going backwards or forwards in time.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: AVOID RE-Printing of Duplicate Values in an Array!

    Quote Originally Posted by gimbal2 View Post
    I have the same problem with dates; I always screw up when needing to validate dates going backwards or forwards in time.
    I used to be quite good with dates in the previous century: I could calculate a (short) Julian day number of any date in that century by head; man, I am stuffed with useless knowledge ;-)

    kind regards,

    Jos (<--- starting at the 359th digit of PI is the number 360)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Printing values from Array on individual lines
    By Castle in forum New To Java
    Replies: 2
    Last Post: 11-28-2012, 01:43 AM
  2. Can 2-3 Trees contain duplicate values?
    By 6thDAY in forum Advanced Java
    Replies: 1
    Last Post: 05-14-2011, 03:47 AM
  3. How to detect duplicate values in an Array?
    By maz09 in forum New To Java
    Replies: 1
    Last Post: 04-08-2010, 07:58 AM
  4. Printing values from object in Array?
    By thesinter in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 05:19 AM
  5. Avoid/Eliminate Duplicate Code
    By kicker in forum New To Java
    Replies: 6
    Last Post: 09-11-2008, 04:14 AM

Posting Permissions

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