Jump to content

Keep duplicates from array


blumi
 Share

Recommended Posts

_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 here
RegExp tutorial: enough to get started
PCRE 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

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

 

Link to comment
Share on other sites

Correct, but _ArrayFindAll runs in O². That won't make a difference in small samples but will explode on serious input.

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 here
RegExp tutorial: enough to get started
PCRE 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

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) &gt; 1;

 

Skysnake

Why is the snake in the sky?

Link to comment
Share on other sites

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

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 by iamtheky

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

Link to comment
Share on other sites

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

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 by iamtheky

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

Link to comment
Share on other sites

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 by iamtheky

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

Maps are faster:

#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

 

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 here
RegExp tutorial: enough to get started
PCRE 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

  • 4 years later...

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

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 here
RegExp tutorial: enough to get started
PCRE 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

  • 1 year later...

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

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