Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Removing duplicates from an array

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Removing duplicates from an array

    I have some code to build an object elementData that stores a list of ints in an array and also keeps track of a boolean unique that determines if duplicates are allowed in a list. I have written a small method to search out duplicates and remove them, but for some reason it is not working. I am not sure if the problem is with the method itself or the placement of the call to the removeDuplicates method.

       // post: places the value in the correct place based on ascending order
       public void add(int value) {
          size++;
          if (size == 1) {  
             elementData[0] = value;
          } else {
             int position =  Arrays.binarySearch(elementData, 0, size - 1, value);
             if (position < 0 ) {
                position = (-position) - 1;
             }
             for (int i = size - 1; i > position; i--) {
                elementData[i] = elementData[i - 1];
             }
             elementData[position] = value;
             if (unique) {
                removeDuplicates();
             }
          }
       }
     
       //post: removes any duplicate values from the list
       private void removeDuplicates() {
          for(int i = size - 1; i < 0; i--) {
             if (elementData[i] == elementData[i - 1]){
                remove(i);
             }
          }
       }
       // pre : 0 <= index < size() (throws IndexOutOfBoundsException if not)
       // post: removes value at the given index, shifting subsequent values left
       public void remove(int index) {
          checkIndex(index);
          for (int i = index; i < size - 1; i++) {
             elementData[i] = elementData[i + 1];
          }
          size--;
       }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Removing duplicates from an array

    Show a sample run that prints the list first filled and then after the attempt to remove duplicates.

Similar Threads

  1. Removing duplicates from an array
    By Parm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2013, 03:23 AM
  2. Advice on removing duplicates from a .txt file while writing to an array
    By shakesgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 19th, 2013, 03:09 PM
  3. Removing Duplicates not working ... new to java
    By jeichenlaubjr in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 25th, 2013, 02:59 PM
  4. merging two unsorted arrays and removing the duplicates..help
    By d4divya2005 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2012, 03:26 PM
  5. Removing duplicates from an Array
    By Rizza in forum Collections and Generics
    Replies: 1
    Last Post: February 21st, 2012, 06:38 PM