how to detect x2 common numbers in a list

Recommended Posts

i got a text file with this number list (see below)

as you can see the number 5 appears twice in this list is there a way we can detect this with any number ? any number appearing more than once in a list to detect it and display a messagebox with that number that appears twice ?

cheers

3

2

5 **

7

9

8

1

5 **

2

Share on other sites

What is the list? Is it a file?

Try creating an array with the size of the largest number available and increment the count of that array for the corresponding number

```#include <Array.au3>
Local \$numbers[] = [3, 2, 5, 7, 9, 8, 1, 5, 2, 3, 2, 9, 1, 33, 22, 1, 4, 33, 8, 0]
Local \$counter[1]
Local \$repeating_numbers = ""

For \$i = 0 to UBound(\$numbers) - 1
If (\$numbers[\$i] > UBound(\$counter)) Then ReDim \$counter[\$numbers[\$i] + 1]
\$counter[\$numbers[\$i]] += 1

If (\$counter[\$numbers[\$i]] = 2) Then \$repeating_numbers &= \$numbers[\$i] & @CRLF
Next

MsgBox("", "Repeating numbers", "These numbers appear more than twice:" & @CRLF & \$repeating_numbers)

_ArrayDisplay(\$counter)```

Share on other sites

the numbers are located inside a txt file, the numbers are one under the other will this work for this case ?

Edited by asiawatcher
Share on other sites

```;\$txt = "3" &@crlf& "1" &@crlf& "2" &@crlf& "3" &@crlf& "4" &@crlf& "41" &@crlf& "42" &@crlf& "5" &@crlf& "6" &@crlf& "42"
;Msgbox(0,"", \$txt)

\$ret = StringRegExpReplace(\$txt, '(?s)(\b\d+\b)(?!.*\b\1\b)\R?', "")
Msgbox(0,"", "duplicates :" & @crlf & \$ret )```

Share on other sites

bravo !!! mikell thanks for once more

Share on other sites

The previous code is basic
You might want to check if there are no duplicates, or if some numbers appear 2 times, 3 times etc
In this case the array way is better

```#Include <Array.au3>

\$txt = "3" &@crlf& "1" &@crlf& "2" &@crlf& "3 " &@crlf& "4" &@crlf& "41" &@crlf& "42" &@crlf& "3" &@crlf& "5" &@crlf& "6" &@crlf& "42"
;Msgbox(0,"", \$txt)

\$all = StringRegExp(\$txt, '(?m)^(\d+)\s*\$', 3)
_ArrayDisplay(\$all)

Local \$u = UBound(\$all), \$nb[\$u][2], \$n
For \$i = 0 to \$u-1
\$txt = StringRegExpReplace(\$txt, '\b' & \$all[\$i] & '\b', "")
If @extended > 1 Then
\$nb[\$n][0] = \$all[\$i]
\$nb[\$n][1] = @extended & " times"
\$n += 1
EndIf
Next
If \$n = 0 Then
Msgbox(0,"", "no duplicates")
Else
Redim \$nb[\$n][2]
_ArrayDisplay(\$nb)
EndIf```

Share on other sites

Or same thing for each value :

```#Include <Array.au3>

Local \$txt = "3" &@crlf& "1" &@crlf& "2" &@crlf& "3 " &@crlf& "4" &@crlf& "41" &@crlf& "42" &@crlf& "3" &@crlf& "5" &@crlf& "6" &@crlf& "42"

Local \$uniq = StringRegExp(\$txt, "(?s)\b(\d+)\b(?!.*\b\1\b)", 3)

Local \$aResult[UBound(\$uniq)][2]
For \$i = 0 To UBound(\$uniq) - 1
StringRegExpReplace(\$txt, "\b" & \$uniq[\$i] & "\b", "")
\$aResult[\$i][0] = \$uniq[\$i]
\$aResult[\$i][1] = @extended & " times"
Next
_ArraySort(\$aResult, 1, 0, 0, 1)
_ArrayDisplay(\$aResult)```

Share on other sites

hi all thanks for your answers is there a way to do it like mihell's style without arrays for numbers appearing twice but NOT 3 times (ignore those numbers) cheers

Share on other sites

only returns the items that appear the number of times specified

```\$nCount = 2

Local \$txt = "3" &@crlf& "1" &@crlf& "2" &@crlf& "3" &@crlf& "4" &@crlf& "6" &@crlf& "42" &@crlf& "3" &@crlf& "5" &@crlf& "6" &@crlf& "42"
\$aList = stringsplit(\$txt , @CRLF , 3)

_ArraySort(\$aList)
\$k = 1

For \$i = ubound(\$aList) - 1 to 0 step -1
If \$i > 0 AND \$aList[\$i]= \$aList[\$i - 1] Then
_ArrayDelete(\$aList , \$i)
\$k += 1
Else
If \$k <> \$nCount Then _ArrayDelete(\$aList, \$i)
\$k = 1
EndIf
Next

_ArrayDisplay(\$aList , \$nCount & " Time(s)")```

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

Share on other sites

and here's a solution with a func i written 2009:

```#include <Array.au3>
#include <File.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
Return \$aUnique2D
EndFunc   ;==>_countUniqueNumbers```

Share on other sites

I was going to suggest _ArrayUnique but AutoBert beat me to the punch.

Share on other sites

2 minutes ago, AutoBert said:

Meanwhile i saw same method using @extended from StringReplace from Melba23 it's shorter (so some ms faster?) and better to read.

Thanks @AutoBert

Create an account

Register a new account