Get item with highest number of duplicates in array

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ddougal
Posts: 52
Joined: 09 Nov 2015, 14:26

Get item with highest number of duplicates in array

25 Apr 2018, 06:43

Purpose: get the item llisted the most. In this case apple
what if there are more than one listed the most say apple and cherry both have 3 duplicates
load file into variable
data file consists of:
orange
cherry
pear
apple
apple
cherry
peach
apple

Code: Select all

file := "data.txt"
;load data into variable
FileRead, LoadedText, %file%

;load array
MatchesArray := []
; Split on new line
myArray := StrSplit(LoadedText, "`n")

;get lines that have apple and store in array
outerConter = 0
innerCounter = 1
;msgbox % MatchesArray[0]
Loop, % MatchesArray.MaxIndex()
{
	outerConter++
	msgbox % MatchesArray[outerConter]
	Loop, % MatchesArray.MaxIndex() 
	{
		if (innerCounter = outerCounter) {
				
		}
		innerCounter++	
		outer := myArray[outerConter]
		inner := myArray[innerCounter]
		msgbox inner: %inner% outer: %outer%
		;msgbox % MatchesArray[outerContrer+1]
	}
}

;search array for highest number of duplicates using loop
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Get item with highest number of duplicates in array

25 Apr 2018, 06:49

What about this?

Code: Select all

str=
(
orange
cherry
pear
apple
apple
cherry
peach
apple
)
Arr := []
loop, Parse, str, `n, `r
{
	if ( !Arr.Haskey(A_LoopField) )
		Arr[A_LoopField] := 1
	else
		Arr[A_LoopField] += 1
}	
for key, val in Arr
	strO .= key "`t" val "`n"
MsgBox % strO
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb and 294 guests