Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to delete all duplicates in an array

Status
Not open for further replies.

ttomasko

Programmer
Aug 6, 2008
28
Hello,

I have an array:

var a = [1,1,2,2,3,4,4,5,6,6];

I need to delete all the duplicates, i.e., both pairs of each duplicate so that I am only left with the elements that are unique.

In the example above the result should be 3, 5.

I've tried pushing or splicing to no avail.

Any suggestions would be great.

Tom
 
Tom,

Show us the code you are using.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
var a = [1,1,2,2,3,4,4,5,6,6];

for(var i = 0; i <a.length-1; i++){
if(a == a[i+1]){
a.splice(a,2);
}//end if
}//end for i
alert(a);
 
Hi

Wrong theory - that has chance to work only if :
[ul]
[li]duplicates occur consecutively[/li]
[li]there are no triplicates or other odd multiplications[/li]
[/ul]
Wrong implementation :
[ul]
[li]you should remove [highlight]i[sup]th[/sup][/highlight] item, not a[sup]th[/sup][/li]
[li]after removing 2 items your loop control variable will be ahead, skipping 1 item, so [highlight pink]step 1 back[/highlight][/li]
[/ul]
JavaScript:
[b]for[/b] [teal]([/teal][b]var[/b] i [teal]=[/teal] [purple]0[/purple][teal];[/teal] i [teal]<[/teal]a[teal].[/teal]length[teal]-[/teal][purple]1[/purple][teal];[/teal] i[teal]++)[/teal] [teal]{[/teal]
  [b]if[/b] [teal]([/teal]a[teal][[/teal]i[teal]][/teal] [teal]==[/teal] a[teal][[/teal]i[teal]+[/teal][purple]1[/purple][teal]])[/teal] [teal]{[/teal]
    a[teal].[/teal][COLOR=darkgoldenrod]splice[/color][teal]([/teal][highlight]i[/highlight][teal],[/teal][purple]2[/purple][teal])[/teal]
    [highlight pink]i[teal]--[/teal][/highlight]
  [teal]}[/teal]
[teal]}[/teal]


Feherke.
 
Thanks very much! I knew I was on the right track but took a wrong fork in the road.

I will be comparing two lists of strings. They will be concatenated and then sorted. I am looking for whatever might have been added to the second list. It would not be possible that there would be triplicates, only duplicates in the concatenated list.

Tom
 
I will be comparing two lists of strings. They will be concatenated and then sorted. I am looking for whatever might have been added to the second list.

Then why not just compare the list2 items to the list1 items and save the list2 items in an array if it does not exist in list1? No need to delete duplicates then.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
This should work as well...
Code:
var a = [1,1,1,1,1,2,2,2,3,4,4,4,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6];
alert(ReturnUniqueSortedList(a));

function ReturnUniqueSortedList(ArrayName){
var tmpObj = new Object;
for (var i in ArrayName) {   
tmpObj[ArrayName[i]] = ArrayName[i];
}
var uniques = new Array;
for (var z in tmpObj) {
   uniques.push(tmpObj[z]);}
return uniques;
}
 
I would be interested in seeing the code for such a comparison. However in this case I'm not sure it it necessary.

In InDesign I will be comparing a list of paragraph styles from each of two documents. In most cases the two lists will be exactly the same. But sometimes someone adds some styles. Rather than trying to eyeball that, I use a script to get the names of each style. Then I will put these two lists--two separate text frames--into a third temporary document. I'll link the frames, meaning it is now one list. The script I am creating will make an array of those style names, sort them, and delete the duplicates which will leave me the names of any added styles.

Hope that explains it.

Tom
 
Hi

mmerlinn is right. No need to concatenate and sort.

If new values may appear only in the second array, is easy to solve it even without a loop :
JavaScript:
[b]var[/b] first [teal]=[/teal] [teal][[/teal][purple]1[/purple][teal],[/teal][purple]2[/purple][teal],[/teal][purple]4[/purple][teal],[/teal][purple]6[/purple][teal]];[/teal]
[b]var[/b] second [teal]=[/teal] [teal][[/teal][purple]1[/purple][teal],[/teal][purple]2[/purple][teal],[/teal][purple]3[/purple][teal],[/teal][purple]4[/purple][teal],[/teal][purple]5[/purple][teal],[/teal][purple]6[/purple][teal]];[/teal]
[b]var[/b] new_in_second [teal]=[/teal] second[teal].[/teal][COLOR=darkgoldenrod]filter[/color][teal]([/teal][b]function[/b][teal]([/teal]v[teal])[/teal] [teal]{[/teal] [b]return[/b] first[teal].[/teal][COLOR=darkgoldenrod]indexOf[/color][teal]([/teal]v[teal])==-[/teal][purple]1[/purple] [teal]}[/teal][teal]);[/teal]
Note that the used JavaScript 1.6 methods are available in the current stable versions of all major browsers. However, in Explorer they were introduced only in version 9.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top