I did something like this a while ago, here is how.
I stored each element name/id in an array
Then when ever a user would add a new element I would re iterate my array
["inputname1", "inputname2", "inputname3"] and replace each name id with the new id number based on how many inputs I have iterated over.
If an input got deleted the array would shift, no worries about same input id number.
More efficient than the original(you dont need to refresh each input remove button or their ids) Example
Sorry if I did not explain well enough.