Jump to content

[SOLVED]Count duplicates in an array


Recommended Posts

Hello everyone,

I just have a beginner question: is there a way to count duplicates in an array? If you don't see what I mean, here is an example of what I mean:

$arr[5] = ['a', 'a', 'b', 'b', 'c'] ; the array

countDuplicates($arr); Representing the data in a bidimensionnal array, this would return:

#cs
['a'][2]
['b'][2]
['c'][1]

Of course this is a bit messy, but that's just for you to get the idea
#ce

I'm trying to achieve this to check whether yes or no an array has more or same amount of occurrences of a character than another array.

Am I doing this the right way?

Does such a function exist?

Thanks for everything :)

Spoiler
Spoiler

 

Edited by yhu420
added solved tag
Link to comment
Share on other sites

There is no func (@Dev's please correct when wrong) to this job. But you can solve like this:

#include <Array.au3>

$txt = ""
For $i=0 to 100
    $txt &= Random(1,30,1)&@CRLF
Next



;$txt = FileRead('test.txt') ;if txt is ia in a file

;startpoint
;ConsoleWrite($txt & @CRLF)
_ArrayDisplay(_countUniqueNumbers($txt), '_countUniqueNumbers')

Func _countUniqueNumbers($sText='Test', $sDelim=@CRLF)
    ;ConsoleWrite($sText & @CRLF)
    ;returns the count of each unique number in a String
    ;autor: autobert (autoit.de) 11/2009
    $sText = @CRLF&StringReplace($sText, ' ', '')
    $sText = StringReplace($sText, $sDelim, $sDelim & ' ')
    $aSource = StringSplit($sText, $sDelim, 3)
    _ArrayDelete($aSource, 0)
    ;_ArrayDisplay($aSource,'Original')
    $aUnique = _ArrayUnique($aSource)
    _ArrayDelete($aUnique, 0)
    Dim $aUnique2D[UBound($aUnique)][2]
    For $x = UBound($aUnique) - 1 To 0 Step -1
        $aUnique2D[$x][0] = $aUnique[$x]
        StringReplace($sText, $aUnique[$x] & $sDelim, 'a')
        $aUnique2D[$x][1] = @extended
        If StringStripWS($aUnique2D[$x][0],8)='' Then _ArrayDelete($aUnique2D,$x)
    Next
    _ArraySort($aUnique2D)
    Return $aUnique2D
EndFunc   ;==>_countUniqueNumbers

 

Link to comment
Share on other sites

Try this :

#Include <Array.au3>

Local $arr = ['a', 'a', 'b', 'b', 'c', 'e', 'f', 'a'] 
Local $aDup = _ArrayCountOcc($arr, 2)
_ArrayDisplay($aDup)

; iOrdByNumber = 0 : Do not sort
;                1 : Sort by number of occurences
;                2 : Sort by number of occurences, in descending order
Func _ArrayCountOcc($aArray, $iOrdByNumber = 0)
    If UBound($aArray, 0) <> 1 Then Return SetError(1, 0, 0)
    If Not IsInt($iOrdByNumber) Or $iOrdByNumber < 0 Or $iOrdByNumber > 2 Then Return SetError(2, 0, 0)
    Local $aRet[UBound($aArray)][2], $last, $iIndex = 0
    _ArraySort($aArray)
    For $i = 0 To UBound($aArray) - 1
        If $aArray[$i] = $last And $i <> 0 Then
            $aRet[$iIndex - 1][1] += 1
        Else
            $aRet[$iIndex][0] = $aArray[$i]
            $aRet[$iIndex][1] = 1
            $last = $aArray[$i]
            $iIndex += 1
        EndIf
    Next
    Redim $aRet[$iIndex][2]
    If $iOrdByNumber Then _ArraySort($aRet, ($iOrdByNumber = 2 ? 1 : 0), 0, 0, 1)
    Return $aRet
EndFunc

 

Edited by jguinch
Link to comment
Share on other sites

Sorry first example was to count in text, this one is for counting in array:

#include <Array.au3>

#cs
Dim $aArray[101]
For $i=0 to 100
    $aArray[$i] = Random(1,30,1)
Next
#ce
Local $aArray[5]    = ["a", "a", "b", "b", "c"]
_ArrayDisplay(_countUniqueElements($aArray), '_countUniqueElements')

Func _countUniqueElements($aSource)
    ;returns the count of each unique element in a Array
    ;autor: autobert
    Local $aUnique = _ArrayUnique($aSource)
    _ArrayDelete($aUnique, 0)   
    Dim $aUnique2D[UBound($aUnique)][2]
    For $x = 0 to UBound($aUnique) - 1 
        $aUnique2D[$x][0] = $aUnique[$x]
        $aCount=_ArrayFindAll($aSource,$aUnique[$x])
        $aUnique2D[$x][1] = UBound($aCount)
    Next
    _ArraySort($aUnique2D)
    Return $aUnique2D
EndFunc   ;==>_countUniqueElements^

 

Link to comment
Share on other sites

#include<array.au3>

Local $aArray[8]    = ["a", "a", "b", "b", "c" , "c" , "c" , "d"]

Local $aOut[0][2]

for $i = ubound($aArray) - 1 to 0 step -1
        $aFound = _ArrayFindAll($aArray , $aArray[$i])
        _ArrayAdd($aOut , $aArray[$aFound[0]] & "|" & ubound($aFound) ,  0)
        _ArrayDelete($aArray , _ArrayToString($aFound , ";"))
        $i -= ubound($aFound) - 1
next

_ArrayDisplay($aOut)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

First of all, sorry for the late reply.

Thanks to all of you who answered, I feel sorry for posting a duplicate thread, but I hope I will help out people asking the same question.

Special thanks to iamtheky, his solution seemed the most effective and everything indeed works fine :D

Thanks again to all of you for helping me out!

Link to comment
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
 Share

×
×
  • Create New...