Solved: Debugging - Replacing Duplicate Items via DesignScript

Good day, Dynamo Forum! This will be a long post ahead, so I humbly ask for your patience and kindness. I’m relatively new to DesignScript, and I’ve taken the big step of challenging myself with writing a function to build my skills and confidence.

The Problem: To write a function to replace duplicates for a list.

I spent a whole day crafting and refining a flowchart to explain the flow and logic of the script, and another day attempting to reflect it properly in the code block:

The logic of the program is designed to:

a. Execute a loop as long as the number of unique items is less than the list count.
b. Check if an item (the Current Item) has a duplicate, starting from the first value.
c. If the Current Item has a duplicate, it will change the value of the duplicate by increasing it with 1. No change will be done to the Current Item.
d. If the Current Item is in the middle of the list and has a duplicate before the middle of the list, the Current Item will be compared with the item next to it until the last item, and then with the first item in the list until it reaches the duplicate or the Current Item itself (if there is no duplicate).
e. If it has no duplicate, it will set the item next to the Current Item as the new Current Item. (If I understand correctly, imperative code blocks specifically allow the logic of points c & e.)
f. It will do these repeatedly until it reaches the last item. Once it reaches the last item (now the new Current Item) it will compare it to all other items to check for duplicates, starting the the 1st item of the list.
g. If there are still duplicates after comparing the last item (i.e., if # unique items < list count), it will start over from the first item of the updated list.
h. The program will terminate ONLY if there are no more duplicates, and then display the final updated list.

And here is the code block:

Questions:

  1. Are the values in the list being properly updated in this script? (I don’t think it is, and I think I’m missing a line to properly update/overwrite the existing values in the list.)
    1.1. Am I misunderstanding how imperative blocks update values in lists?
    1.2. Should I use “List.ReplaceItemAtIndex” instead?
  2. Are the “return CI” lines unnecessary? (I think this is so, please confirm or correct me. I initially thought those lines would return the Current Values and update the list, until there are no more duplicates)
    2.1. Should I actually return anything else within the imperative code block other than the updated list?

If there is any other problem that you can see, please tell me. Although programming is definitely not my comfort zone, I actually enjoyed dissecting the problem and trying to reflect it in the code. I’m sure I would enjoy programming more if I get over this hurdle, so any help would mean a lot to me.

Thank you!

Edit 1: Changed the photo of the flowchart, since the initial one was a wrong copy.

I think you could significantly simplify your logic if you could better define what it is you’re after.

It sounds like you want to remove all duplicate values from a list by incrementing each duplicate until there are only unique values. The simple version would be to check each value for a duplicate, increment the duplicate, and then check to see if that value exists as a duplicate as well and continue incrementing until a new value is found. This would not take into account the duplicate position however. If you only want to increase the value of the duplicate “down-list” of the current value, then you can separately track the previous values and the future values.

It would help more if you could show us an example of exactly what you’re expecting. Hopefully working out one scenario will help all of us (and you) understand the intended outcome for something like this.

1 Like

Hello, @Nick_Boyts! Thank you so much. I will follow your advice and try to restructure the logic of my function and brainstorm other approaches. I’ll go back to the drawing board.

Basically, for a given list of Revit elements which have duplicates, I want to use the node List.IndexOf to return the true indices of duplicate items. However, it only returns the index of the first (zero-based) instance of the value.

After giving it much thought and trying to compare the actual output list that I want with the erroneous list that List.IndexOf is giving me, I think incrementing values is the wrong approach. I really appreciate you telling me to simplify the logic. I’ve been barking up the wrong tree all this time.

I solved the problem now with the help of some nodes. Turns out I didn’t need to write code at all! I still want to learn how to write and solve problems in DesignScript, though.

Thank you so much! Here are some screenshots for reference (I couldn’t export my whole graph as an image because it’s too big, hahaha):

Solved:

1 Like