【每日一题0107】去重到底

:mage:t2: 给定一个由纯数字组成的列表,其中可能存在多个重复的数字,请编写一个函数,将相同数字中先出现的数字移除,只保留该重复数字的最后一个。返回去重后的列表。

示例:
输入:[3, 4, 4, 3, 6, 3],输出:[4, 6, 3]。因为对于数字3,索引是0和3的会被移除;对于数字4,索引1的会被移除。

题目难度:简单
题目来源:CodeWars: Simple remove duplicates

def solution(nums: list) -> list:
    # your code

assert solution([3,4,4,3,6,3]) == [4,6,3]
assert solution([1,2,1,2,1,2,3]) == [1,2,3]
assert solution([1,2,3,4]) == [1,2,3,4]
def solution(nums):
    result_list=[]
    for num in nums[::-1]:
        if num not in result_list:
            result_list.append(num)
    return result_list[::-1]
2 Likes

def test(data:list):
new =
for i in data[::-1]:
if i not in new:
new.append(i)
new.reverse()
return new

def solution(nums: list) -> list:
    nums.reverse()
    num = []
    for n in nums:
        if n not in num:
            num.append(n)
    num.reverse()
    return num
def solution(nums: list) -> list:
    return sorted(list(set(nums[::-1])), key=nums[::-1].index)[::-1]


assert solution([3, 4, 4, 3, 6, 3]) == [4, 6, 3]
assert solution([1, 2, 1, 2, 1, 2, 3]) == [1, 2, 3]
assert solution([1, 2, 3, 4]) == [1, 2, 3, 4]
def solution(nums):
    return list({}.fromkeys(nums[::-1]).keys())[::-1]
2 Likes
def solution(nums: list) -> list:
    nums2 = [x for x in nums]
    for i in range(len(nums) - 1):
        if nums[i] in nums[i + 1:]:
            nums2.remove(nums[i])
    return nums2


assert solution([3, 4, 4, 3, 6, 3]) == [4, 6, 3]
assert solution([1, 2, 1, 2, 1, 2, 3]) == [1, 2, 3]
assert solution([1, 2, 3, 4]) == [1, 2, 3, 4]

def solution(nums: list) -> list:
    l = []
    for i in range(len(nums)-1,-1,-1):
        if nums[i] not in l:
            l.insert(0,nums[i])
    return l
def solution15(nums:list):
    result=[]
    for i in nums[::-1]:
        if i not in result:
            result.insert(0,i)
    return result
assert solution15([3,4,4,3,6,3]) == [4,6,3]
assert solution15([1,2,1,2,1,2,3]) == [1,2,3]
assert solution15([1,2,3,4]) == [1,2,3,4]

def Removal():
list = [3, 4, 4, 3, 6, 3]
listRemoval=
for i in list:
if i not in listRemoval:
listRemoval.append(i)
print(listRemoval)
Removal()

def solution(nums: list) -> list:
    aft_nums = []
    for idx, num in enumerate(nums):
        if num not in nums[idx + 1:]:
            aft_nums.append(num)
    return aft_nums


assert solution([3, 4, 4, 3, 6, 3]) == [4, 6, 3]
assert solution([1, 2, 1, 2, 1, 2, 3]) == [1, 2, 3]
assert solution([1, 2, 3, 4]) == [1, 2, 3, 4]
def solution(nums: list) -> list:
    print(nums[::-1])
    n = 0
    m = len(nums) - 1
    while n < m:
        for i in range(0, len(nums)-1):
            if nums[i] in nums[i+1:]:
                nums.pop(i)
                break
        n += 1
    return nums
def solution(nums: list) -> list:
    # 方法一
    nums.reverse()
    new = []
    for i in nums:
        if i not in new:
            new.append(i)
    new.reverse()
    return new
    
    # 方法二
    # 利用 fromkeys 创建新字典,seq 为反转后的 nums
    # 这样会得到自动去重后的k-v,只取 key,再做反转
    return list(dict.fromkeys(nums[::-1]).keys())[::-1]
    
    # 方法三
    # 利用 sorted 排序规则,按照反转后的列表下标排序
    return sorted(list(set(nums[::-1])), key=nums[::-1].index)[::-1]

方法 2 和方法 3 是看到评论区的解法,学到了,:+1:

def solution(nums: list) -> list:
    result=[]
    for i in nums:
        if i in result:
            result.remove(i)
        result.append(i)
    return result
assert solution([3,4,4,3,6,3]) == [4,6,3]
assert solution([1,2,1,2,1,2,3]) == [1,2,3]
assert solution([1,2,3,4]) == [1,2,3,4]
``
def solution(nums: list) -> list:
    # your code
    for i in set(nums):
        while nums.count(i) > 1:
            nums.remove(i)
    return nums
def solution(nums: list) -> list:
    return [x for i,x in enumerate(nums) if nums[i+1:].count(x)==0]

def Removal():
list = [3, 4, 4, 3, 6, 3]
list2 =
for i in list:
if i not in list2:
list2.append(i)
elif i in list2:
list2.remove(i)
list2.append(i)
print(list2)
Removal()

关闭