Delete BOTH duplicates from a LIST

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
Chipp
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 27
Joined: Sat Apr 08, 2006 9:50 am
Location: Austin, TX
Contact:

Delete BOTH duplicates from a LIST

Post by Chipp » Sun Oct 12, 2014 5:33 am

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!
[url=http://www.altuit.com/webs/hemingway/Products/AltuitProducts.htm]Visit Altuit, creator of ButtonGadget and other Rev add-ons[/url]

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 89
Joined: Thu Apr 13, 2006 6:25 pm

Re: Delete BOTH duplicates from a LIST

Post by rkriesel » Sun Oct 12, 2014 6:50 am

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

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 795
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Delete BOTH duplicates from a LIST

Post by Thierry » Sun Oct 12, 2014 7:55 am

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)
Regex LiveCode sunnYrex
https://sunny-tdz.com

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Delete BOTH duplicates from a LIST

Post by Simon » Sun Oct 12, 2014 8:02 am

Hi Chipp,
Very fun stuff here:
http://forums.livecode.com/viewtopic.ph ... lit=+split

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 795
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Delete BOTH duplicates from a LIST

Post by Thierry » Sun Oct 12, 2014 8:30 am

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
Regex LiveCode sunnYrex
https://sunny-tdz.com

Chipp
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 27
Joined: Sat Apr 08, 2006 9:50 am
Location: Austin, TX
Contact:

Re: Delete BOTH duplicates from a LIST

Post by Chipp » Sun Oct 12, 2014 9:22 am

Wow guys, thanks! :D EXACTLY what I was looking for!
[url=http://www.altuit.com/webs/hemingway/Products/AltuitProducts.htm]Visit Altuit, creator of ButtonGadget and other Rev add-ons[/url]

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5246
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Delete BOTH duplicates from a LIST

Post by richmond62 » Sun Oct 12, 2014 11:32 am

Really primitive proof of concept:

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

Download the file called "weeding.zip"

Post Reply

Return to “Talking LiveCode”