Jump to content

Count number of same items within an array.


 Share

Recommended Posts

Hey folks.

I have (another) seemingly simple problem with an array. I want to count the number off all same items and output the result. The result for the uploaded example array would be:

0: 5

1: 1

3: 7

4: 3

5: 5

7: 1

8: 3

 

Hope the problem is not new and someone knows a solution.

Best wishes.

error471

example.png

Edited by error471
Link to comment
Share on other sites

  • Moderators

Something like this, perhaps?

#include <Array.au3>

Local $aArray[7] = [5, 1, 7, 3, 5, 1, 3]
Local $aResult = _ArrayFindAll($aArray, 5)
    MsgBox($MB_OK, "Number of 5's in Array", UBound($aResult))

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

error471,

 I know you use my GUIListViewEx UDF, so just use that to get the ListView content into an array and then loop through each item, adding one to the relevant count in a separate array as each item is checked:

#include <GUIConstantsEx.au3>
#include <Array.au3>

#include "GUIListViewEx.au3"

Global $aLV_Array[][] = [["Kent, Clark", 3, 8, 8, 7, 3], _
        ["Wayne, Bruce", 3, 1, 3, 3, 3], _
        ["Parker, Peter", 0, 3, 4, 5, 0], _
        ["Duck, Donald", 0, 5, 4, 4, 0], _
        ["Mouse, Mickey", 0, 5, 5, 5, 8]]

$hGUI = GUICreate("Test", 500, 500)

$cLV = GUICtrlCreateListView("Col 0             |Col 1|Col 2|Col 3|Col 4|Col 5", 10, 10, 480, 300, $LVS_SINGLESEL)
_GUICtrlListView_SetExtendedListViewStyle($cLV, $LVS_EX_FULLROWSELECT)

For $i = 0 To 4
    $sData = ""
    For $j = 0 To 5
        $sData &= $aLV_Array[$i][$j] & "|"
    Next
    GUICtrlCreateListViewItem(StringTrimRight($sData, 1), $cLV)
Next

$cGo = GUICtrlCreateButton("Go", 10, 450, 80, 30)

GUISetState()

$iLVIndex = _GUIListViewEx_Init($cLV, $aLV_Array)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cGo
            $aRet = _GUIListViewEx_ReturnArray($iLVIndex)
            $aCount = _CountItems($aRet)
            _ArrayDisplay($aCount, "", Default, 8)
    EndSwitch

WEnd

Func _CountItems($aArray)

    Local $aRet[10]

    For $i = 0 To UBound($aArray) - 1
        For $j = 1 To UBound($aArray, 2) - 1
            $iValue = $aArray[$i][$j]
            $aRet[$iValue] += 1
        Next
    Next

    Return $aRet

EndFunc

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Another (simple) method:
 

#include <Array.au3>

Local $aArray = [['Kent, Clark',3,8,8,7,3], _
                ['Wayne, Bruce',3,1,3,3,3], _
                ['Parker, Peter',0,3,4,5,0], _
                ['Duck, Donald',0,5,4,4,0], _
                ['Mouse, Mickey',0,5,5,5,8]]

_ArrayDisplay($aArray, 'Original Array')

Local $iRows = UBound($aArray)
Local $iCols = UBound($aArray, 2)

Local $aData[$iRows * ($iCols -1)]
Local $iCount = 0

For $i = 0 To $iRows -1
    For $j = 1 To $iCols -1
        $aData[$iCount] = $aArray[$i][$j]
        $iCount += 1
    Next
Next

$aData = _ArrayUnique($aData)
; _ArrayDisplay($aData)

Local $aOccurrence[$aData[0]][2]
For $i = 1 To $aData[0]
    $aOccurrence[$i -1][0] = $aData[$i]
    $aOccurrence[$i -1][1] = 0
    For $j = 0 To $iRows -1
        For $k = 1 To $iCols -1
            If $aArray[$j][$k] = $aOccurrence[$i -1][0] Then $aOccurrence[$i -1][1] += 1
        Next
    Next
Next
_ArrayDisplay($aOccurrence, 'Occurrence')

 

Edited by czardas
Link to comment
Share on other sites

18 minutes ago, czardas said:

Notice that my method ignores Col 0. It was also written in a bit of hurry, so you might want to make modifications to it: for the exercise if not for any other reason.

Ignoring 0 was exactly what I needed. ;)

Link to comment
Share on other sites

  • Moderators

error471,

Just for info, so does my code - as I knew that is what you wanted!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

It's an extremely common occurrence, although I consider using the first row, or column, as headers to be specific usage as opposed to general usage. I am writing some functions at the moment that would swallow the code I wrote above in a single mouthful. I hope to have them finished soon. :)

Link to comment
Share on other sites

And another method.

#include <Array.au3>
; Modified from ; https://www.autoitscript.com/forum/topic/180314-how-to-detect-x2-common-numbers-in-a-list/?do=findComment&comment=1294363

Local $ret
Global $aLV_Array[][] = [["Kent, Clark", 3, 8, 8, 7, 3], _
        ["Wayne, Bruce", 3, 1, 3, 3, 3], _
        ["Parker, Peter", 0, 3, 4, 5, 0], _
        ["Duck, Donald", 0, 5, 4, 4, 0], _
        ["Mouse, Mickey", 0, 5, 5, 5, 8]]

Local $txt = _ArrayToString($aLV_Array, @LF)
Local $uniq = StringRegExp($txt, "(?s)\b(\d+)\b(?!.*\b\1\b)", 3) ; Unique digits only in this array.
_ArraySort($uniq)
;_ArrayDisplay($uniq)

For $i = 0 To UBound($uniq) - 1
    StringRegExpReplace($txt, "\b" & $uniq[$i] & "\b", "")
    $ret &= $uniq[$i] & ":" & @extended & @LF
Next

ConsoleWrite(StringTrimRight($ret, 1) & @LF) ; Remove trailing @LF

#cs ; Returns:-
0:5
1:1
3:7
4:3
5:5
7:1
8:3
#ce

 

Link to comment
Share on other sites

Things get complicated. The values are no numbers anymore, but decimals. The correct solution would be:

0,5: 1

1: 2

1,5: 4

2: 3

2,5: 2

3,5: 3

4: 1

5: 1

5,5:1

9: 1

9,1: 3

9,3: 1

11: 2

 

I hope this is a common problem and someone already has a solution.

example.png.cb9f256dd05e6e55c7f21eb1499081c1.png

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...