สร้างลิสต์ใหม่ที่ไม่มีค่าซ้ำจากลิสต์เดิมโดยเช็คก่อน append
ตัวอย่าง
Input: [2,3,4,3,4,6,78,90] Output: [2,3,4,6,78,90]
อัลกอริทึม
- สร้างลิสต์ว่าง newlist
- วนลูปอิลิเมนต์ในลิสต์เดิม
- ถ้ายังไม่มีใน newlist ให้ append
โค้ดตัวอย่าง
def removeduplicateele(A):
newlist = []
for n in A:
if n not in newlist:
newlist.append(n)
return newlist
A = []
n = int(input("Enter the size of the List: "))
for _ in range(n):
A.append(int(input()))
print("THE NEW LIST IS:", removeduplicateele(A))
ผลลัพธ์
Enter the size of the List: 5 Enter the numbers: 10 20 30 20 10 THE NEW LIST IS: [10, 20, 30]