yhu420 Posted March 26, 2016 Posted March 26, 2016 (edited) 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 March 27, 2016 by yhu420 added solved tag
MichaelHB Posted March 26, 2016 Posted March 26, 2016 I belive this two links will help you achive your goal. https://www.autoitscript.com/forum/topic/162076-search-an-array-for-duplicates/ https://www.autoitscript.com/forum/topic/165182-counting-and-sorting-duplicate-lines/ coles 1
AutoBert Posted March 26, 2016 Posted March 26, 2016 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
jguinch Posted March 26, 2016 Posted March 26, 2016 (edited) 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 March 26, 2016 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
AutoBert Posted March 26, 2016 Posted March 26, 2016 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^
iamtheky Posted March 26, 2016 Posted March 26, 2016 #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) Mingre and Gianni 2 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
UEZ Posted March 27, 2016 Posted March 27, 2016 Maybe this helps: Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
yhu420 Posted March 27, 2016 Author Posted March 27, 2016 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 Thanks again to all of you for helping me out!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now