X-Received: by 10.66.250.233 with SMTP id zf9mr10189280pac.12.1381641496209; Sat, 12 Oct 2013 22:18:16 -0700 (PDT) X-Received: by 10.50.2.74 with SMTP id 10mr251714igs.15.1381641496027; Sat, 12 Oct 2013 22:18:16 -0700 (PDT) Path: csiph.com!usenet.pasdenom.info!usenet.blueworldhosting.com!feeder02.blueworldhosting.com!news.glorb.com!z6no52087054pbz.1!news-out.google.com!9ni31659qaf.0!nntp.google.com!o2no1900201qas.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.help Date: Sat, 12 Oct 2013 22:18:15 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=24.22.233.44; posting-account=g0ZuQQoAAADSeOkjOVZiSpiYG1HO4qDe NNTP-Posting-Host: 24.22.233.44 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: How to remove duplicate values in an array. From: miss.smileyfish@gmail.com Injection-Date: Sun, 13 Oct 2013 05:18:16 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.java.help:2801 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 dup= licates 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 =3D=3D 1) { =20 elementData[0] =3D value; } else { int position =3D Arrays.binarySearch(elementData, 0, size - 1, va= lue); if (position < 0 ) { position =3D (-position) - 1; } for (int i =3D size - 1; i > position; i--) { elementData[i] =3D elementData[i - 1]; } elementData[position] =3D value; if (unique) { removeDuplicates(); } } } =20 //post: removes any duplicate values from the list private void removeDuplicates() { for(int i =3D size - 1; i < 0; i--) { if (elementData[i] =3D=3D elementData[i - 1]){ remove(i); } } } // pre : 0 <=3D index < size() (throws IndexOutOfBoundsException if not) // post: removes value at the given index, shifting subsequent values le= ft public void remove(int index) { checkIndex(index); for (int i =3D index; i < size - 1; i++) { elementData[i] =3D elementData[i + 1]; } size--; }