Delete duplicate string characters in a array

The algorithm does not know what you want it to do, and it won’t do something that you don’t write in code. If you think through the algorithm step by step, you notice that the nested recursive invocation of list_of_definitions does not know the outer invocation’s defs_name array. Instead, it creates a new array. So you put them into many arrays, not into a single array. Because of that, the first (outer) defs_name array does not contain nested definition names.

To learn more about recursion, see here and this wikipedia article.

To collect all definition names into the same array, you need to share the array between all method invocations. Instead of using an instance/class variable (= side effects), a better functional way is to pass the shared array as argument in the recursive call, as I did above. The first function invocation must be given an empty array.

def list_of_definitions(ents, def_names)
  …
    list_of_definitions(e.definition.entities, def_names)
…
def_names = []
list_of_definitions(instances, def_names)
p(def_names)
2 Likes