Jump to content
blumi

Keep duplicates from array

Recommended Posts

blumi

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]

Share this post


Link to post
Share on other sites
jchd

_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)

Share this post


Link to post
Share on other sites
rootx

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

 

  • Like 2

Share this post


Link to post
Share on other sites
jchd

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

  • Like 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 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)

Share this post


Link to post
Share on other sites
Skysnake

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?

Share this post


Link to post
Share on other sites
iamtheky

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)

 


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

Share this post


Link to post
Share on other sites
iamtheky

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

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

Share this post


Link to post
Share on other sites
kylomas

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

Share this post


Link to post
Share on other sites
iamtheky

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

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

Share this post


Link to post
Share on other sites
iamtheky

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

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

Share this post


Link to post
Share on other sites
LarsJ

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

 

  • Like 2

Share this post


Link to post
Share on other sites
jchd

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

 

  • Like 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 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)

Share this post


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

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.