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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for duplicate values in a list

Status
Not open for further replies.

aleci

Programmer
May 22, 2001
40
GB
For my purposes a valid list cannot duplicate values:

<cfset list1 = &quot;1,2,3,4,5&quot;>

<cfset list2 = &quot;1,2,3,3,5&quot;>

Therefore how can i check these to allow list1 but not list2?
Been looking at listfunctions all day and now im fed up.
Thanks in advance.


 
Is it always numeric?

1,2,3,4,5,6,7,8,9,10,11,12?

Single digit?

0,1,2,3,4,5,6,7,8,9?

Single Character?

0,1,2,3,4,5,6,7,8,9,A,B,C,D etc?

If its always numeric but not single digit, do you have a way of identifying the last number? Did I help?
Vote!
 
Oh, if its numeric but not necessarily single digit, would like 33,717,344 be allowed?

Tony Did I help?
Vote!
 
Hi tony,
Yes the list will always be numeric and can contain double characters:

1,2,3,4,5,6,7,8,9,10,11,12.

Also 33,717,344 would be allowed since no number in the list is repeated.

Yes i can identify the last number but how would this help?

Also to note - the numbers un the list may not increase in value:

1,4,5,6,2,3 would be a valid list!

Any ideas?




 
Solved - This does the job:

<cfset mylist=&quot;5,8,9,10,105,9,15&quot;>

<cfset sorted_list = ListSort(mylist,&quot;numeric&quot;)>

<CFSET first = ListGetAt (sorted_list, 1)>

<cfset last_item = FIRST - 1>

<cfloop list=&quot;#sorted_list#&quot; index=&quot;current_item&quot;>

<cfif current_item EQ last_item>

<CFSET DUPLICATE = &quot;THERE IS A DUPLICATE - #current_item#&quot;>

<CFELSE>

<cfset last_item = current_item>

</cfif>

</cfloop>

<CFIF ISDEFINED (&quot;DUPLICATE&quot;)>
<cfoutput>#DUPLICATE#</cfoutput>
</cfif>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top