• 0

[Classic ASP] Only show duplicates from array


Question

I have an array, and I'd like to show ONLY the duplicates.

Array = {1,3,2,1,4,1,4}

It would return back only 1 and 4.

I know this code works for getting rid of duplicates -

function RemDups(byVal anArray)
	dim d, item, thekeys
	set d = CreateObject("Scripting.Dictionary")
	d.removeall
	d.CompareMode = 0
	for each item in anArray
		if not d.Exists(item) then d.Add item, "1"
	next
	thekeys = d.keys
	set d = nothing
	RemDups = thekeys
end function

Thanks for the help! :woot:

1 answer to this question

Recommended Posts

  • 0

You could just extend that function slightly:

	if not d.Exists(item) then d.Add item, "1"

	If Not(d.Exists(item)) Then
	  Call d.Add(item, 1)
	Else
	  d.Item(item) = d.Item + 1 ' to increment for this value
	End If
Next
Dim dup
Set dup = CreateObject("Scripting.Dictionary")
For Each item In d.Keys
	If Not(d.Item(item) = 1) Then Call dup.Add(item, 1) ' if it isn't 1 then it must be a duplicate
Next
Set d = Nothing

the duplicates should now be stored as dup.Keys

Caveat: I haven't used the Dictionary object in about 4 years, so the above code might not work but should point you in the right direction.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.