blumi Posted December 9, 2017 Share Posted December 9, 2017 How to keep the duplicates from an array? [1,1,2,3,3,3,3,4,5,5] the Result should be [1,3,5] Link to comment Share on other sites More sharing options...
rootx Posted December 9, 2017 Share Posted December 9, 2017 https://www.autoitscript.com/autoit3/docs/libfunctions/_ArrayUnique.htm Link to comment Share on other sites More sharing options...
jchd Posted December 9, 2017 Share Posted December 9, 2017 _ArrayUnique isn't the right answer here, since the OP wants to keep only a single instance of all entries which have dups in the input array. _ArrayUnique would include 4, which is single, hence shouldn't be selected. @blumi, I'd convert the array into a Map datatype (supported in AutoIt beta) keeping the count of multiple instances and converting back entries with count > 1 into an output array. This will work faster than scanning the array in O² and runtime will remain unaffected by array order. AutoIt beta has been out for long enough to be considered stable eough for practical purposes. If you're still reluctant about beta, use a scripting dictionary object instead. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
rootx Posted December 9, 2017 Share Posted December 9, 2017 I'm sorry I have read to fast!!! Your solution.. #include<array.au3> Local $aArray[10] = [1,1,2,3,3,3,3,4,5,5] for $i = ubound($aArray) - 1 to 0 step -1 $aFound = _ArrayFindAll($aArray , $aArray[$i]) $i -= ubound($aFound) - 1 If ubound($aFound) > 1 Then ConsoleWrite("This Value "&$aArray[$aFound[0]] & " Is duplicate N Times " & ubound($aFound)&@CRLF) EndIf next Skysnake and Earthshine 2 Link to comment Share on other sites More sharing options...
jchd Posted December 9, 2017 Share Posted December 9, 2017 Correct, but _ArrayFindAll runs in O². That won't make a difference in small samples but will explode on serious input. Earthshine 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Skysnake Posted December 9, 2017 Share Posted December 9, 2017 For large volume data, use a SQLite memory db? CREATE TABLE `mytable` ( `value` integer, `id` integer PRIMARY KEY AUTOINCREMENT ); ------------------------ -- [1,1,2,3,3,3,3,4,5,5] insert into mytable (value) values (1) , (1) , (2) , (3) , (3) , (3) , (3) , (4) , (5) , (5) ; ----------------------------- select value, count(value) from mytable group by value having count(value) > 1; Returns value count(value) "1" "2" "3" "4" "5" "2" 3 rows returned in 18ms from: select value, count(value) from mytable group by value having count(value) > 1; Skysnake Why is the snake in the sky? Link to comment Share on other sites More sharing options...
mikell Posted December 9, 2017 Share Posted December 9, 2017 ? #include <Array.au3> Local $a[11] = [1,5,2,3,3,3,3,4,1,5,6] _ArraySort($a) Local $b[UBound($a)], $n For $i = 0 to UBound($a)-2 If $a[$i] = $a[$i+1] Then $b[$n] = $a[$i] $n += 1 EndIf Next ReDim $b[$n] $b = _ArrayUnique($b, 0, 0, 0, 0) _ArrayDisplay($b) Link to comment Share on other sites More sharing options...
iamtheky Posted December 10, 2017 Share Posted December 10, 2017 dont know how it compares to the redim penalty, but here is an array dance that does it in one loop #include<array.au3> Local $aArray[10] = [1,1,2,3,3,3,3,4,5,5] _ArraySort($aArray) For $i = ubound($aArray) - 1 to 1 step -1 If $aArray[$i] = $aArray[$i - 1] Then _ArrayDelete($aArray , $i) $i -= 1 If $i = 0 then exitloop $flag = 1 EndIf If $aArray[$i] <> $aArray[$i - 1] And $flag = 0 Then _ArrayDelete($aArray , $i) ElseIf $aArray[$i] <> $aArray[$i - 1] And $flag = 1 Then $flag = 0 EndIf If $flag = 1 then $i += 1 Next _ArrayDisplay($aArray , $i) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
iamtheky Posted December 10, 2017 Share Posted December 10, 2017 (edited) Also, this ($2 contains the non-duplicates) Local $aArray[10] = [1,1,2,3,3,3,3,4,5,5] $flag = 0 $str = StringRegExpReplace(_ArrayToString($aArray , "") , "(?:(.)\1+)+|(.)" , "$1") msgbox(0 , '' , $str) Edited December 10, 2017 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
kylomas Posted December 10, 2017 Share Posted December 10, 2017 iamthekey, Try it like this... #include <array.au3> Local $aArray[20] = [1,1,2,3,3,3,3,4,5,5,6,6] $str = StringRegExpReplace(_ArrayToString($aArray , "") , "(?:(.)\1+)+|(.)" , "$1") msgbox(0 , '' , $str) Trying to understand the pattern to see where it is failing. kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
iamtheky Posted December 10, 2017 Share Posted December 10, 2017 (edited) its a straight copy/paste from https://stackoverflow.com/questions/29480579/regex-finding-non-duplicate-characters I have no idea why it works/doesnt work either. **And its all sorts of broke, if you add another 4 to your array it only returns 1 and 6 Edited December 10, 2017 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
iamtheky Posted December 10, 2017 Share Posted December 10, 2017 (edited) awww shit, think i fixed it... ;~ Local $aArray[20] = [1,1,2,2,2,3,3,3,3,4,4,4,4,4,4,5,6,6] ;~ Local $aArray[20] = [1,1,2,3,3,3,3,4,5,5,6,6] Local $aArray[20] = ["a",1,2,3,3,3,3,"a",4,5,5,6,6] _ArraySort($aArray) $str = StringRegExpReplace(_ArrayToString($aArray , "") , "(.).*\1|(.)" , "$1") edit: bear in mind this only works for sorted arrays, with single character values, or rather the first character of entries. (take off the sort and the last example returns undesired values). someone smart can probably make it more extensible. Edited December 10, 2017 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
LarsJ Posted December 10, 2017 Share Posted December 10, 2017 For the general solution (not limited to integers, not limited to single character values, not limited to sorted arrays) you'll not find anything faster than this. Try yourself. #include <Array.au3> Global $aArray[10] = [1,1,2,3,3,3,3,4,5,5] Global $aRes = Example( $aArray ) _ArrayDisplay( $aRes ) Global $iItems = 100000 ReDim $aArray[$iItems] For $i = 0 To $iItems - 1 Step 2 $aArray[$i] = $i/2 $aArray[$i+1] = $i/2 Next _ArrayDisplay( $aArray ) $aRes = Example( $aArray ) _ArrayDisplay( $aRes ) Func Example( $aArray ) Local $oTst = ObjCreate( "Scripting.Dictionary" ) Local $oRes = ObjCreate( "Scripting.Dictionary" ) For $i = 0 To UBound( $aArray ) - 1 If Not $oTst.Exists( $aArray[$i] ) Then $oTst( $aArray[$i] ) = 1 ElseIf Not $oRes.Exists( $aArray[$i] ) Then $oRes( $aArray[$i] ) = 1 EndIf Next Return $oRes.Keys() EndFunc Skysnake and TheDcoder 2 Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
jchd Posted December 10, 2017 Share Posted December 10, 2017 Maps are faster: expandcollapse popup#include <Array.au3> Global $iItems = 100000 Global $aArray[$iItems] For $i = 0 To $iItems - 1 $aArray[$i] = Random(0, $iItems * 0.2, 1) Next Local $t, $aRes _ArrayDisplay( $aArray ) $t = TimerInit() $aRes = Example( $aArray ) ConsoleWrite(TimerDiff($t) & @LF) _ArrayDisplay( $aRes ) $t = TimerInit() $aRes = MapExample( $aArray ) ConsoleWrite(TimerDiff($t) & @LF) _ArrayDisplay( $aRes ) Func Example( ByRef $aArray ) Local $oTst = ObjCreate( "Scripting.Dictionary" ) Local $oRes = ObjCreate( "Scripting.Dictionary" ) For $i = 0 To UBound( $aArray ) - 1 If Not $oTst.Exists( $aArray[$i] ) Then $oTst( $aArray[$i] ) = 1 ElseIf Not $oRes.Exists( $aArray[$i] ) Then $oRes( $aArray[$i] ) = 1 EndIf Next Return $oRes.Keys() EndFunc Func MapExample(ByRef $aArray) Local $mRes[] For $i = 0 To UBound($aArray) - 1 If MapExists($mRes, $aArray[$i]) Then $mRes[$aArray[$i]] += 1 Else $mRes[$aArray[$i]] = 1 EndIf Next For $i = 0 To UBound($aArray) - 1 If $mRes[$aArray[$i]] = 1 Then MapRemove($mRes, $aArray[$i]) EndIf Next Return $mRes.Keys() EndFunc Skysnake and pixelsearch 2 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Champak Posted July 11, 2022 Share Posted July 11, 2022 I'm looking in the help file (v3.3.16.0) and it still says maps are experimental and may be removed and this is a 5 year old thread. I would think after 5 years it's no longer experimental. Before I use this, was that overlooked and it's good to go, or is the map feature still not fleshed out? Link to comment Share on other sites More sharing options...
jchd Posted July 11, 2022 Share Posted July 11, 2022 AFAICT all quirks have been fixed long ago. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Champak Posted July 12, 2022 Share Posted July 12, 2022 thanks Link to comment Share on other sites More sharing options...
Champak Posted October 3, 2023 Share Posted October 3, 2023 I've been playing with this and can't figure a way to ALSO return triplicates, and not increase the time if only looking for duplicates? I have a search function that has an option to filter the search with 2 or 3 conditions, so sometimes I need it to return based on 3 similar indexes being found. Right now I'm just running the function twice, although it is still relatively fast, I'm hoping it can be combined to reduce the time. Func _MapSearch(ByRef $aArray, $bDupeReturn = False) Local $mRes[] For $i = 0 To UBound($aArray) - 1 If MapExists($mRes, $aArray[$i]) Then $mRes[$aArray[$i]] += 1 Else $mRes[$aArray[$i]] = 1 EndIf Next If $bDupeReturn = False Then For $i = 0 To UBound($aArray) - 1 If $mRes[$aArray[$i]] = 1 Then MapRemove($mRes, $aArray[$i]) EndIf Next Else For $i = 0 To UBound($aArray) - 1 If $mRes[$aArray[$i]] <> 1 Then MapRemove($mRes, $aArray[$i]) EndIf Next EndIf Return $mRes.Keys() EndFunc Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now