Jump to content
Sign in to follow this  
Fran

Duplicate value in array

Recommended Posts

Fran

Wonder if someone can help me with this please... my brain is in Friday-mode ;)

I have an array that I want to check for duplicate values and then warn the user. Below is an example extract from my code:

$sArray = StringSplit("123,456,789,321,654,789,987", ",")
$cntSamples = $sArray[0]

;if <there is a duplicated value in the array> then
MsgBox(4096, "Error", "There are duplicate values in the array.") ;and echo the value

EDIT: I'm sorry for posting on this forum.. I didn't realise when I made the post. And now I don't know how to delete this and post on the general forum. :)

Edited by Fran

Share this post


Link to post
Share on other sites
water

I would use something like this:

#include <array.au3>
$aArray = StringSplit("123,456,789,321,654,789,987", ",")
_ArrayDisplay($aArray)

; Only tells if there are differences
$aNewArray = _ArrayUnique($aArray, 1, 1)
_ArrayDisplay($aNewArray)
If $aArray[0] <> $aNewArray[0] Then MsgBox(4096, "Error", "There are duplicate values in the array.")

; Shows the duplicate values
_ArraySort($aArray, 0, 1)
_ArrayDisplay($aArray)
For $index = 1 To $aArray[0]-1
    If $aArray[$index] = $aArray[$index+1] Then MsgBox(4096, "Error", "Value '" & $aArray[$index] & "' is a duplicate value in the array")
Next

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Fran

You are legend! Thank you ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×