Clean duplicate in list

Shay

Well-Known Member
Licensed User
Longtime User
Hi

how can I remove duplicate lines in list
or prevent adding line that already exist into list

Thanks
 

stevel05

Expert
Licensed User
Longtime User
To check if an item exists use indexof:

B4X:
       If thisList.IndexOf(NewItem) = -1 Then
       thisList.Add(NewItem)
       End If

If the list has duplicates and contains all strings or numbers, you can sort it using List.Sort then run through it from the end and remove duplicates in one pass.

If it contains other objects then it will take longer as you will have to check each one individually against the whole list to see if it's duplicated (again start from the end) and remove it if it's appears elsewhere.

With a bit of luck someone will have a better solution, but it'll get you going.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Just a thought, you could write all the items in the list to a map, then recreate the list from the map. A map will only allow unique keys, you could use anything as data and ignore it when recreating your list. I don't know if it will be quicker than comparing all the items, especially if you can't sort them.
 
Upvote 0
Top