## How to return an array position Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
songdg
Posts: 57
Joined: 04 Oct 2017, 20:04

### How to return an array position

An array consist of seven elements,I want to return the position like this

Code: Select all

``````;if all the elements is the same return the first position
;return the first position of the elements which occur most
arr := [111, 111, 111, 111, 111, 111, 111]      ;return 1
arr := [123, 111, 111, 111, 111, 111, 111]      ;return 2
arr := [112, 119, 111, 111, 111, 111, 111]      ;return 3
arr := [117, 116, 115, 111, 111, 111, 111]      ;return 4
arr := [117, 118, 111, 111, 189, 111, 111]      ;return 3
arr := [117, 119, 111, 111, 111, 111, 177]      ;return 3``````
wolf_II
Posts: 2446
Joined: 08 Feb 2015, 20:55

### Re: How to return an array position  Topic is solved

Try this:

Code: Select all

``````;if all the elements is the same return the first position
;return the first position of the elements which occur most
arr1 := [111, 111, 111, 111, 111, 111, 111]      ;return 1
arr2 := [123, 111, 111, 111, 111, 111, 111]      ;return 2
arr3 := [112, 119, 111, 111, 111, 111, 111]      ;return 3
arr4 := [117, 116, 115, 111, 111, 111, 111]      ;return 4
arr5 := [117, 118, 111, 111, 189, 111, 111]      ;return 3
arr6 := [117, 119, 111, 111, 111, 111, 177]      ;return 3

Loop, 6
MsgBox, % PosOf_MostItem(arr%A_Index%)

ExitApp

;-------------------------------------------------------------------------------
PosOf_MostItem(List) { ; return first position of the most frequent item in List
;-------------------------------------------------------------------------------
Table := []
For index, Candidate in List
Table[Candidate].Score++
Else
Table[Candidate] := {Score: 1, firstOccurrance: index}

Maximum := 0
For each, Entry in Table
If (Entry.Score > Maximum)
Maximum := Entry.Score
, indexOf_Max := Entry.firstOccurrance

Return, indexOf_Max
}
``````
I hope that helps.
songdg
Posts: 57
Joined: 04 Oct 2017, 20:04

### Re: How to return an array position

wolf_II wrote:Try this:

Code: Select all

``````;if all the elements is the same return the first position
;return the first position of the elements which occur most
arr1 := [111, 111, 111, 111, 111, 111, 111]      ;return 1
arr2 := [123, 111, 111, 111, 111, 111, 111]      ;return 2
arr3 := [112, 119, 111, 111, 111, 111, 111]      ;return 3
arr4 := [117, 116, 115, 111, 111, 111, 111]      ;return 4
arr5 := [117, 118, 111, 111, 189, 111, 111]      ;return 3
arr6 := [117, 119, 111, 111, 111, 111, 177]      ;return 3

Loop, 6
MsgBox, % PosOf_MostItem(arr%A_Index%)

ExitApp

;-------------------------------------------------------------------------------
PosOf_MostItem(List) { ; return first position of the most frequent item in List
;-------------------------------------------------------------------------------
Table := []
For index, Candidate in List
Table[Candidate].Score++
Else
Table[Candidate] := {Score: 1, firstOccurrance: index}

Maximum := 0
For each, Entry in Table
If (Entry.Score > Maximum)
Maximum := Entry.Score
, indexOf_Max := Entry.firstOccurrance

Return, indexOf_Max
}
``````
I hope that helps.
Thanks,much appreaciated.