RemDups Function:
RemDups removes duplicates from an array and returns a new array with no duplicates.
Description:
RemDups removes duplicates from an array and returns a new array with no duplicates. Does not modify the existing array.
Syntax:
array = RemDups(array)
Example:
<%
function ShowArray(byval arr) dim item, s for each item in arr s = s & item & "<BR>" next ShowArray = s end function
dim theArray
'--- set up a test with a number array.......
theArray = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0,_ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,_ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,_ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,_ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
'--- show array before modifications response.write "before:<HR>" & showArray (theArray) & "</HR>"
'--- remove duplicate numbers theArray = RemDups(theArray)
'--- show the array with no duplicate values response.write "after:<HR>" & showArray (theArray) & "</HR>"
'--- set up a test with a string array.......
theArray = Array("ox", "horse", "cow", "pig", _ "deer", "monkey", "rabbit", _ "ox", "dog", "cat", "pig", _ "dog", "rabbit", "frog", _ "cat", "horse")
'--- show array before modifications response.write "before:<HR>" & showArray (theArray) & "</HR>"
'---- remove duplicate string values theArray = RemDups(theArray)
'--- show the array with no duplicate values response.write "after:<HR>" & showArray (theArray) & "</HR>"
%>
ASP Source Code:
<%
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, item next thekeys = d.keys set d = nothing RemDups = thekeys end function
%>
|