数组中重复的数字(Python)

#!/usr/bin/env python 
# -*- coding: utf-8 -*-
# @Time    : 2019-08-13 22:35
# @Author  : daryl
# @File    : main.py


def use_map(numbers):
    """
    使用哈希表结构
    :param numbers:
    :return:
    """
    num_map = dict()
    for number in numbers:
        if number in num_map:
            print(number)
        else:
            num_map[number] = True


def use_set(numbers):
    """
    使用set结构
    :param numbers:
    :return:
    """
    num_set = set()
    for number in numbers:
        if number in num_set:
            print(number)
        else:
            num_set.add(number)


def use_sort(numbers):
    """
    排序后查找
    :param numbers:
    :return:
    """
    numbers = sorted(numbers)
    print(numbers)
    for index, number in enumerate(numbers):
        if index != number:
            continue
        if number == numbers[number]:
            print(number)
        else:
            numbers[index], numbers[number] = numbers[number], numbers[index]


if __name__ == '__main__':
    numbers = [2, 3, 1, 0, 2, 5, 3]
    # use_map(numbers)
    # use_set(numbers)
    use_sort(numbers)

posted @ 2019-08-17 17:49  darylc  阅读(1675)  评论(0编辑  收藏  举报