How to add elements beginning in a specific position?

José Nobre :

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.

ordonezalex :

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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=311315&siteId=1