(Jun-14-2019, 04:10 PM)ThomasL Wrote: @calonia Modifying a list whilst iterating over it is a very bad idea.
The 2nd example from @metulburr is perfect as it keeps the order of the items in the original list.
If that doesn´t matter his 1st example is faster.
In addition your runtime is O(len(numbers)*len(numbers)) which is bad.
numbers.count(x) runs over the full list and counts each number.
Try to do this in O(len(numbers) which is possible. (not using above examples) :-)
thanks for the reply. i really appreciate your help. but i want to know what is wrong with this code. it doesn't seem to work with some of the numbers!