Remove duplicates from array (Python)

11 篇文章 0 订阅

1. Given an input integer array, get all distinct elements in it (not necessary to keep their original relative order).

def distinct(arr):
    if not arr:
        return None
    return list(set(arr))

Follow-up questions:

(1) What is the time complexity of your algorithm?
(2) Time complexity of set construction operation, set query operation, and techniques to deal with collisions.

2. Get the first occurrence of each distinct element.

def distinct(arr):
    if not arr:
        return None

    # Resulting list.
    result = []

    # Elements that have already been added to the resulting list.
    added = set()

    for ele in arr:
        if ele not in added:
            added.add(ele)
            result.append(ele)

    return result

Follow-up questions:

(1) What is the time complexity of your algorithm?

3. Get the second occurrence (if exists, otherwise get the only occurrence) of each distinct element.

def distinct(arr):
    if not arr:
        return None


    from collections import Counter

    result = []
    cnt = Counter(arr)
    occ = dict()
    for ele in arr:
        if cnt[ele] == 1 or (ele in occ and occ[ele] == 1):
            result.append(ele)
        if ele in occ:
            occ[ele] += 1
        else:
            occ[ele] = 1
    return result

Follow-up questions:

(1) What is the time complexity of your algorithm?
(2) How to solve the problem using only one intermediate data structure (i.e. remove occ)?
(3) How is Counter implemented, and what’s the performance difference between list and Counter?

4. Generic version: Given an array of integers (may have duplicates), retrieve all the nth last occurrence (if exists else return the last occurrence) of each distinct element.

# Optimised solution.
def distinct(arr, n):
    """
    Time Complexity: O(n), n is the size of array.
    Space Complexity: O(n), n is the size of array.
    """
    if not arr:
        return None


    from collections import Counter, deque

    cnt = Counter(arr)
    d = deque()
    for index in xrange(len(arr) - 1, -1, -1):
        ele = arr[index]
        if cnt[ele] == n or (cnt[ele] > 0 and cnt[ele] < n):
            d.appendleft(ele)
            cnt[ele] = 0
        else:
            cnt[ele] -= 1
    return list(d)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值