Don't have an account? Register now to chat, post, use our tools, and much more.
Latest Headlines
Online Users
There are 82 users online: 5 members, 52 guests and 25 bots. Members: ACagliano, Kllrnohj. Bots: Spinn3r (2), MSN/Bing (1), VoilaBot (2), Googlebot (19), MSN/Bing (1).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
|
| Author |
Message |
|
kpa4941

New Member

Joined: 23 Nov 2010 Posts: 84 Location: Carrollton, Texas
|
Posted: 18 Jan 2012 11:35:36 am Post subject: How do you remove duplicate values from a list? |
|
|
Let's say that I have a list that contains (1,2,2,3,4).
How would I get rid of the duplicate 2 to make the list look like (1,2,3,4)?
Keep in mind that the list could possibly contain 20 or so duplicates with different numbers.
Thanks. _________________ --kpa4941 |
|
| Back to top |
|
|
shkaboinka

Power User

Joined: 30 Jun 2010 Posts: 372 Location: Spokane, WA
|
Posted: 18 Jan 2012 12:18:25 pm Post subject: |
|
|
If it's okay to sort the list, you can do this (and I will pretend that the list is in L1):
1: use SortA(L1) or SortD(L1) to sort the list
2: Clear another list which you will copy the values into
3: Loop through the list, and only when an item is different than the previous item, add it to the new list. This works because all the duplicates will be next to eachother (since the list is sorted).
You can do this by storing the last item in a variable which you can use to compare the next item to. Here is the code, if you like (ignore if you want to figure it out by yourself):
If 2>dim(L1
Return
SortA(L1
{L1(1->L2
Not(L1(1->A
For(X,2,dim(L1
If A#L1(X
augment(L2,L1(X->L2
End
L2->L1
Last edited by shkaboinka on 18 Jan 2012 12:25:41 pm; edited 1 time in total |
|
| Back to top |
|
|
Xeda112358
Power User

Joined: 09 Nov 2010 Posts: 354
|
Posted: 18 Jan 2012 12:19:34 pm Post subject: |
|
|
Hmm, I am pretty sure I saw a neat idea on TIBD that could do this, I will see if I can find it ... (I doubt I will)
EDIT: This is from Timothy Foster on TIBD:
| Quote: |
I got one also:
SortA(L1
L1+E25i(L1=augment(ΔList(cumSum(L1)),{eπ→L1
SortA(L1
dim(L1)-sum(0 xor imag(L1→dim(L1
I think it is a tad larger than Xeda's, but it doesn't use For( and uses no additional lists. It also uses xor
Edit: I should note that this may be skimpy on very large numbers. Anything over E25 will probably fail. You could try and change 25 to a larger number in the program, but it somehow produces overflow errors at a certain point that I didn't bother checking for.
|
(TIBD is TI-BASIC Developer) |
|
| Back to top |
|
|
Weregoose

Cemetech Expert

Joined: 23 Oct 2009 Posts: 464
|
|
| Back to top |
|
|
Xeda112358
Power User

Joined: 09 Nov 2010 Posts: 354
|
Posted: 19 Jan 2012 08:03:46 pm Post subject: |
|
|
| Haha, awesome! I like that code, though I also like the use of xor in the other code, too XD |
|
| Back to top |
|
|
shkaboinka

Power User

Joined: 30 Jun 2010 Posts: 372 Location: Spokane, WA
|
Posted: 19 Jan 2012 10:05:25 pm Post subject: |
|
|
Weregoose's code is excellent because it does not affect another list variable at all! (I thought that Ans would be destroyed by the "For", but if that works, that's awesome!) Anyway, if L1 is not sorted ahead of time, then you have to add "SortD(L1" to the start of that for it to work |
|
| Back to top |
|
|
Xeda112358
Power User

Joined: 09 Nov 2010 Posts: 354
|
Posted: 19 Jan 2012 11:25:52 pm Post subject: |
|
|
| Yeah, For( does not modify Ans (which is very useful for algorithms). |
|
| Back to top |
|
|
Weregoose

Cemetech Expert

Joined: 23 Oct 2009 Posts: 464
|
|
| Back to top |
|
|
shkaboinka

Power User

Joined: 30 Jun 2010 Posts: 372 Location: Spokane, WA
|
Posted: 21 Jan 2012 12:12:15 am Post subject: |
|
|
| How does that work out!? Each time it's comparing just to the min value in the list, and what guarantees that the current value ("Xth") in the loop is unique if it is not equal to the min value so far? I would suppose that would only be true if the list was sorted in decending order...? |
|
| Back to top |
|
|
Weregoose

Cemetech Expert

Joined: 23 Oct 2009 Posts: 464
|
Posted: 21 Jan 2012 06:30:16 am Post subject: |
|
|
Code: :{L1(1
:For(X,2,dim(L1
:If min(Ans≠L1(X
:augment(Ans,{L1(X
:End
:Ans→L1
Line 1: Deal Ans a list of a single element equal to L1's first element.
Line 2: With X taking on, one per pass, each of the integers 2 through the number of elements in L1:
Line 3: [PASS BEGIN] – Ask each of Ans's existing elements if it is unequal to the Xth element of L1—meanwhile returning a list of TRUE and FALSE responses as 1 and 0—and then commit the minimum of those responses to the inquiry, "Is this nonzero?"
Line 4: If "yes," copy the Xth element of L1 to form a new element of Ans.
Line 5: [PASS END]
Line 6: Overwrite L1 using Ans.
About line 3: This is a method of telling whether L1(X) is in Ans when you don't have a single command for that purpose. If any one of the elements in Ans is equal to the number being tested for inclusion, the question of "not equal" returns FALSE, which outputs a zero into the list of which a minimum is taken. As this min( would be in opposition to the "nonzero" implied by the If, the subsequent action is ignored. Nothing of this or any conditional statement updates Ans. _________________ Common Errors in English · How To Ask Questions The Smart Way |
|
| Back to top |
|
|
shkaboinka

Power User

Joined: 30 Jun 2010 Posts: 372 Location: Spokane, WA
|
Posted: 21 Jan 2012 04:48:55 pm Post subject: |
|
|
| ...oh... I was reading it as "min(Ans)≠L1(X" (because for some reason, my "leave off the parenthesis" mechanism applied the invisible ")" to before the "≠") ... That is an AWESOME technique! I've not used whole-list operations much, so I should give them a closer look (I knew they existed, but never thought how to apply them). |
|
| Back to top |
|
|
|
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.
» Go to Registration page
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
© Copyright 2000-2013 Cemetech & Kerm Martian :: Page Execution Time: 0.031179 seconds.
|