Cho một list có phần tử trùng, nhiệm vụ tạo list khác chứa các phần tử không trùng.
Ví dụ
A::[2,3,4,3,4,6,78,90] Output::[2,3,4,6,78,90]
Thuật toán
Bước 1: Tạo list. Bước 2: Tạo list mới rỗng. Bước 3: Duyệt từng phần tử trong list. Bước 4: Nếu phần tử chưa có trong list mới thì true. Bước 5: Thêm vào list mới. Bước 6: Hiển thị list mới.
Ví dụ mã
# Xóa phần tử trùng lặp
def removeduplicateele(A):
newlist = []
for n in A:
if n not in newlist:
newlist.append(n)
return newlist
# Driver Code
A = list()
n = int(input("Enter the size of the List ::"))
print("Enter the number ::")
for i in range(int(n)):
k = int(input(""))
A.append(int(k))
print("THE NEW LIST IS ::>", removeduplicateele(A))
Kết quả
Enter the size of the List ::5 Enter the number :: 10 20 30 20 10 THE LIST IS ::> [10, 20, 30]