Grasshopper

algorithmic modeling for Rhino

# Filter out duplicate numbers in a list?

Is there an easy way to filter out duplicate numbers in a list?

So there is "1,1,0,2,4,4,4,3,4,2,0" and output would be only "0,1,2,3,4".

What would be even better is if there is a way to also remove duplicate objects in a list, or objects with the same geometry, is that possible?

Much appreciated.

Theunis

Views: 20702

### Replies to This Discussion

For numbers i usually sort them ascending, and then using a small algorithm remove the duplicate entries, something along the lines of this:

1.First use the sort component to sort the values
2. Plug the result into a vb.net component in x, remember to set x to list.
3. Use the following algorithm, or something alike it

Dim numbers as new list(of double)

'Zero or some value you are sure will not be in your list
dim last_val as double = 0

for i as int32 = 0 to x.count()-1

if not x(i) = last_val then

last_val = x(i)

end if

next

a=numbers

I haven't tried it out, i just wrote the code here, so bear with me if it does not work and you have to make some correction.

You can do something similar with points and vectors where you check their coordinates against eachother, for other geometry, it will be somewhat more complicated i think.

/Thomas
Hi Thomas and Theunis,

thanks for your post,

I couldn't get the script to work but it would be amazing if it could delete specific items from a list which is hard to do with GH alone(i.e. If i want to get rid of all the items which have 0 as an index).

When i copy pasted the script it i got the message hereafter.
It would be great if as y we could plug the item index that we want to get rid of in the list...

thanks again,

Arthur

This is how you can do it entirely in Grasshopper. The logic here is:

1. Create a list of incremental numbers with the same length as your input (an index map). This will allow us to maintain list order after we're done.

2. Sort your input number while synchronously sorting the index map. Identical numbers are now adjacent in the list, but since we also sorted the index map we know the permutation that was needed to convert the unsorted list into the sorted one.

3. Shift your sorted numbers one to the right (using Wrap=False). This now gives you another list (one item shorter) which you can compare with the sorted list using an Equals component. We'll use the NotEquals output to create a culling pattern which will remove all duplicates from the sorted list.

4. Before we do so however, we need to prepend a single value (True) to the culling pattern, since shifting has resulting in a pattern that is one element too short. Since we know that the first item in any list is always unique, we can prepend a true value.

5. Cull both the sorted list and the sorted index map.

6. Sort again, this time using the mangled index map as Keys to put the original list back into order again.

A problem with this approach is that sorting does not pay attention to the initial order of identical items. Thus, if you have four equal numbers in your original list, there's no telling in what order they end up.

--
David Rutten
david@mcneel.com
Seattle, WA
A tacky way to solve that last problem could be to create another list of numbers (or modify the index list) with a very small step size, something like 0.001. Then add it to the original list and use the resulting list for sorting. The first of the duplicated numbers will always pop on top.
Could you maybe give an example please? I would like to think I understand your logic but cannot seem to get it right.

Thanks Vicente
Sure:

Nice stars, thanks for the example. It will be of great help.
Thanks for the great reply David. This will help me out in quite a few instances. One example where I want to use this is to filter out duplicate points via the Decompose Component and applying this to the X,Y and Z numbers. I cannot think of another way to do it for now.
It's easy enough for me to make a component that removes duplicates from lists. Be it numbers, points or strings. Curves, BReps and meshes are a bit harder, but also doable.

--
David Rutten
david@mcneel.com
Seattle, WA
David, could you please explain how to remove duplicate points?

I have a pair of adjoining triangles, and would like to delete one of each of the vertices that they share. The ultimate goal is to create an outline of the parallelogram created by the two; am I barking up the right tree?

Thanks!

Hi Lian:
Try this definition.
Attachments:
Hi, did you finally develop this component?
Thank you

• View All

## Videos

• ### Laser cut designs - Parametric Lamp 2365

Added by Parametric House

• ### Grasshopper Bullet Physics Implementation Test4

Added by Petras Vestartas

• ### Introducing the Grasshopper3d Course

Added by Parametric House