I want to add an array
of Elements
beginning in an x position.
Given my start list
val myList = muttableListOf(1,2,3,4,5)
Given my second list
val mySecondList = muttableListOf(1,2,3,4,5,5,6,7,8,9)
I want to merge both lists, and avoid repeated numbers like
val mergedList = muttableListOf(1,2,3,4,5,1,2,3,4,5,6,7,8,9)
The list I really want is val mergedList = muttableListOf(1,2,3,4,5,6,7,8,9)
How can I achieve this without "tricks" to delete repeated elements and doing it by checking if the element are already there? I will add java
in the keywords because both more or less the same list methods.
I know you are using lists in your question, but if you do not care about order, then you can use sets:
val myList = mutableSetOf(1, 2, 3, 4, 5)
val mySecondList = setOf(1, 2, 3, 4, 5, 5, 6, 7, 8, 9)
myList.addAll(mySecondList)
If you do care about order, then you can use a tree set:
val myTree = TreeSet(setOf(1, 2, 3, 4, 5))
val mySet = setOf(1, 2, 3, 4, 5, 5, 6, 7, 8, 9)
myTree.addAll(mySet)