Page 1 of 1

Delete BOTH duplicates from a LIST

Posted: Sun Oct 12, 2014 5:33 am
by Chipp
Hi all,

I'm trying to find a very fast way to delete BOTH duplicates from a list. For instance if I have a list:

apple
orange
apple
apple
banana

I want to return

orange
banana

I know about the array workaround, but it keeps ONE of them. Other than brute force (repeat for each line L), is there any *faster* way to do this?

Thanks!

Re: Delete BOTH duplicates from a LIST

Posted: Sun Oct 12, 2014 6:50 am
by rkriesel
Hi, Chipp.

Your example suggests you meant ALL, not just both.

If you want to exclude any item that appears more than once in a list, you could do it like this:

Code: Select all

on mouseUp
    local tList
    put "apple,orange,apple,apple,banana" into tList
    replace comma with cr in tList
    get getLinesThatAppearOnlyOnce( tList )
    breakpoint
end mouseUp

function getLinesThatAppearOnlyOnce pList
    local tCountForLine
    repeat for each line tLine in pList
        add 1 to tCountForLine[ tLine ]
    end repeat
    repeat for each key tLine in tCountForLine
        if tCountForLine[ tLine ] > 1 then
            delete variable tCountForLine[ tLine ]
        end if
    end repeat
    return the keys of tCountForLine
end getLinesThatAppearOnlyOnce
Is that what you seek? Is it fast enough?

-- Dick

Re: Delete BOTH duplicates from a LIST

Posted: Sun Oct 12, 2014 7:55 am
by Thierry
I'm trying to find a very fast way to delete BOTH duplicates from a list.
Hello Chip,

Here are 2 versions... (with exactly the same result)

Code: Select all

function F1 L1
   local L2,  A
   repeat for each line L in L1
      add 1 to A[ L]
   end repeat
   repeat for each key K in A
      if A[ K] > 1 then next repeat
      put K &cr after L2
   end repeat
   -- delete last char of L2 -- maybe needed?
   return L2
end F1

Code: Select all

function F2 L1
   sort L1
   get replacetext( L1, "(?m)^(.*)(\n?\n\1)+$", empty)
   filter lines of IT without empty
   return IT
end F2
Have a nice sunday,

Thierry

PS: coding the 2nd one was more fun 8)

Re: Delete BOTH duplicates from a LIST

Posted: Sun Oct 12, 2014 8:02 am
by Simon
Hi Chipp,
Very fun stuff here:
http://forums.livecode.com/viewtopic.ph ... lit=+split

Simon

Re: Delete BOTH duplicates from a LIST

Posted: Sun Oct 12, 2014 8:30 am
by Thierry
Thierry wrote:

Code: Select all

function F2 L1
   sort L1
   get replacetext( L1, "(?m)^(.*)(\n?\n\1)+$", empty)
   filter lines of IT without empty
   return IT
end F2
and get rid of the filter..

Code: Select all

function F2 L1
   sort L1
   return replacetext( L1,  "(?m)^(.*)(\n?\n\1)+(\n|$)" , empty)
end F2

Re: Delete BOTH duplicates from a LIST

Posted: Sun Oct 12, 2014 9:22 am
by Chipp
Wow guys, thanks! :D EXACTLY what I was looking for!

Re: Delete BOTH duplicates from a LIST

Posted: Sun Oct 12, 2014 11:32 am
by richmond62
Really primitive proof of concept:

https://www.dropbox.com/sh/ja47l87gg87s ... C5ORa?dl=0

Download the file called "weeding.zip"