Sign in to follow this  
Followers 0
sublimnl

How to remove duplicate elements from an array?

4 posts in this topic

As the topic states, I need a way to remove duplicate elements from a single dimensional array. I am trying to populate a combo box using an ini file as my source. The layout of the section of the ini file in question is...

[sites]

172.16.4=HOUSTON

172.16.29=PERTH

172.16.25=UK

172.16.17=DELFT

172.16.20=KL

172.16.181=RIO

There is more to it than that as there is more than one subnet for each site. So in the code below, I read the entire "Sites" section to the array $inisites, and then use a For loop to move the site names into a single dimensional array called $sitearray. Now I need a way to yank out all the dupes. Have tried a few things (see the 2nd For Loop below), but keep winding up with dimension range errors in the middle of execution. Anyone have any ideas? I found UDF that claims to remove duplicates from an array, but that did nothing at all.

Thanks!

CODE
$inisites = IniReadSection("Ale.ini","Sites")

Dim $sitearray[$inisites[0][0]]

$sitearray[0] = $inisites[0][0]

For $i = 1 To $inisites[0][0]

$sitearray[$i] = $inisites[$i][1]

Next

For $j = 1 To $sitearray[0]

For $i = 1 To $sitearray[0]

MsgBox(4096,"","$i=" & $i & @CRLF & $sitearray[$j] & @CRLF & $sitearray[$i]) ; debugging purposes

If $sitearray[$j] = $sitearray[$i] Then

_ArrayDelete($sitearray,$i)

EndIf

Next

Next

$sitelist = _ArrayToString($sitearray,"|")

GUICtrlSetData($cmbsite,$sitelist)

Share this post


Link to post
Share on other sites



#include <array.au3>

Global $array[16] = [15, 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 9, 9, 0]

$mynewarray = dupecheckerthingy($array)

_ArrayDisplay($mynewarray, '')

Func dupecheckerthingy($showmethearray)
    Local $tmparray[1] = ['']
    For $i = 1 To UBound($showmethearray) - 1
        _ArraySearch($tmparray, $showmethearray[$i])
        If @error Then _ArrayAdd($tmparray, $showmethearray[$i])
    Next
    $tmparray[0] = 'Number of elements = ' & UBound($tmparray) - 1
    Return $tmparray
EndFunc

Share this post


Link to post
Share on other sites

you rock. thanks!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here is my version, its not required any includes (Array.au3):

Func _ArrayDeleteClones($sArray)
    If Not IsArray($sArray) Then Return SetError(1)
    If UBound($sArray, 0) > 1 Then Return SetError(2)
    Local $NewArr[1], $IsFound=0, $NewArrCnt=0, $Extended=0
    For $i = 1 To UBound($sArray) - 1
        $IsFound = 0
        For $j = 1 To UBound($NewArr)-1
            If $sArray[$i] = $NewArr[$j] Then
                $IsFound = 1
                $Extended += 1
                ExitLoop
            EndIf
        Next
        If Not $IsFound Then
            $NewArrCnt += 1
            ReDim $NewArr[$NewArrCnt+1]
            $NewArr[$NewArrCnt] = $sArray[$i]
        EndIf
    Next
    $NewArr[0] = UBound($NewArr) - 1
    Return SetError(0, $Extended, $NewArr)
EndFunc

EDIT: A litle edit, now it's also return an @Extended that include number of duplicates that was removed from the array. And also a litle error checking returns.

Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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  
Followers 0