Jump to content

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

 

Link to comment
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)

 

Link to comment
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)

;$txt = FileRead("test.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

:)

Link to comment
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)

 

Link to comment
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)")

 

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

Link to comment
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

 

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...