• 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.
  • Posts

  • Recent Achievements

    • Experienced
      dismuter went up a rank
      Experienced
    • One Month Later
      mevinyavin earned a badge
      One Month Later
    • Week One Done
      rozermack875 earned a badge
      Week One Done
    • Week One Done
      oneworldtechnologies earned a badge
      Week One Done
    • Veteran
      matthiew went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      684
    2. 2
      ATLien_0
      266
    3. 3
      Michael Scrip
      196
    4. 4
      +FloatingFatMan
      181
    5. 5
      Steven P.
      142
  • Tell a friend

    Love Neowin? Tell a friend!