Python: Iterate through list and remove duplicates (without using Set())
The issue is with "automatic" for loops - you have to be careful about using them when modifying that which you are iterating through. Here's the proper solution:
def remove_dup(a):
i = 0
while i < len(a):
j = i + 1
while j < len(a):
if a[i] == a[j]:
del a[j]
else:
j += 1
i += 1
s = ['cat','dog','cat','mouse','dog']
remove_dup(s)
print(s)
Output: ['cat', 'dog', 'mouse']
This solution is in-place, modifying the original array rather than creating a new one. It also doesn't use any extra data structures.
Remove duplicates from array of Int in Swift (for-in-loop)
The problematic part is to iterate over an array and update that array at the same time. In this case, removing an element while iterating.
Removing an element decreases array length (count
) and also changes indices. Therefore in
for j in i + 1 ..< array.count {
if array[i] == array[j] {
newArray.remove(at: j)
}
}
After removing the first index, your other indices become invalid. Note that count
is always read only once, before the actual iteration.
This is one of the reasons why removing elements during iteration is dangerous and complicated. You could fix it by maintaining the number of removed elements and update indices accordingly. Or you can iterate backwards:
var newArray = array
for i in (0 ..< newArray.count - 1).reversed() {
for j in (i + 1 ..< newArray.count).reversed() {
if newArray[i] == newArray[j] {
newArray.remove(at: j)
}
}
}
return newArray
You are still changing indices and count
but since you are iterating backwards, you only change the indices that have been already used.
In general it's simple and safer to build a new array instead of updating the current one:
var newArray: [Int] = []
for value in array {
if !newArray.contains(value) {
newArray.append(value)
}
}
return newArray
which can be very reduced in complexity (performance) by using a Set
to keep added elements:
var newArray: [Int] = []
var foundElements: Set<Int> = []
for value in array {
if foundElements.insert(value).inserted {
newArray.append(value)
}
}
return newArray
Which can be simplified using filter
:
var foundElements: Set<Int> = []
return array.filter { foundElements.insert($0).inserted }
Remove Duplicates in a List Python (Using For loop)
When you remove a number and add a zero to the end of the list, your main loop (for i ...) will eventually get to the zeroes at the end. At that point you will have removed one '1' and two '5' so the count will be 3 and there will be 3 zeroes to process. Each of these zeroes will find two other zeroes in the list (replacement with another zero wont make a difference). So, in total 1+2+2+2+2 = 9 counting 1 (for the 1's) 2 (for the 5's) and three more times 2 (for each of the zeroes).
One way around this (without changing your code too much) would be to break the main loop when you reach a zero (assuming zero is not a legitimate value in the initial list)
remove duplicate arrays in loop
You need to loop backwards through the array, and delete the current item.
$length = count($items);
for($i = $length - 1; $i > 0; $i--){
if($items[$i]['part_number'] == $items[$i-1]['part_number']){
unset($items[$i]);
}
}
remove duplicates in a string using nested while loop
OrderedDict
can do something like that:
import collections
print(''.join(collections.OrderedDict.fromkeys('Arpitr')))
Test: https://ideone.com/8lCPec
However if you explicitly want a fix for your code (and also with lower()
),
def removeduplicates(String):
result = ''
list_of_char = []
for i in String:
list_of_char.append(i)
k = 0
print(len(list_of_char))
while k < len(list_of_char)-1:
l = k + 1
while l < len(list_of_char):
if list_of_char[k].lower() == list_of_char[l].lower():
del list_of_char[l]
else:
l = l + 1
k = k + 1
for j in list_of_char:
result = result + j
return result
print(removeduplicates('Arpraprapiiiiraptr'))
Test here: https://ideone.com/BxGBhR
The two main things:
- characters have to be tested starting from the next character to the current one, meaning
l
(or perhapsb
) has to be reset inside the outer loop - when a character is deleted, its position has to be checked again, so the index is not increased.
I want to remove duplicate from list (without for loop)
You can use a Counter
and a conditional list comprehension or filter
in order to maintain the original order:
from collections import Counter
c = Counter(a)
clean_a = filter(lambda x: c[x] == 1, a) # avoids 'for' ;-)
# clean_a = list(filter(lambda x: c[x] == 1, a)) # Python3, if you need a list
# clean_a = [x for x in a if c[a] == 1] # would be my choice
Related Topics
Count Overlapping Regex Matches in Perl or Ruby
How to Know If an Io Is Empty Without Reading It
Webpack Error on Heroku Deployment
How to Mimic Browser X509 Client Certificate Verification Without Access to Http Layer
Rails "Template Is Missing" Error
Rails 3 Translations Within Models in Production
Prawn Gem: How to Create the .Pdf from an *Existing* File (.Xls)
Iterating Over the Registers of a Yardoc '@Macro'
How to Merge Two Equally Sized Arrays into One Array with Sub-Arrays of Merged Values
Rails Undefined Method 'Errors' for Nil:Nilclass in Simple_Form
How Does Rails Know My Timezone
Heroku: Push Rejected, Failed to Compile Ruby App
Escaping Parameters in Set_Form_Data Post
Transfer Db from One Heroku App to Another Faster
Should I Specify Exact Versions in My Gemfile