Hướng dẫn how do you find duplicate numbers in python? - làm thế nào để bạn tìm thấy các số trùng lặp trong python?

Đưa ra một danh sách các số nguyên với các yếu tố trùng lặp trong đó. Nhiệm vụ là tạo ra một danh sách khác, chỉ chứa các yếu tố trùng lặp. Nói một cách đơn giản, danh sách mới nên chứa các yếu tố xuất hiện nhiều hơn một.

Examples:

Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
Output : output_list = [20, 30, -20, 60]
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]

Phương pháp 1: Sử dụng cách tiếp cận vũ lựcUsing the Brute Force approach

Python3

def Repeat(x):

    _size __ len(x)

    

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
1=
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
3

    

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
5
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
6
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
7
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
8
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
9

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
0
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
1=
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
6
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
4
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
5

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
0
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
5
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
8
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
7
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
8def1

def2def3 def4=__

Repeat(x):3Repeat(x):4

    Repeat(x):6 Repeat(x):7

Repeat(x):8=     0    1    2    3    225555552253____5222

_size 5_size 6    22.

len1 len2

Phương pháp 2: Sử dụng hàm bộ đếm () từ mô -đun thu thập Using Counter() function from collection module

Python3

len3 len4len5 len6

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
22=
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
24

len1

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
26

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
27=
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
29
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
30__15

len1

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
40

Đầu ra

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]

Phương pháp 3: Sử dụng phương thức Count ()

Python3

E

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
76=
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
78

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
5
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
80
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
7
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
29
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
83

    

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
85=
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
29
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
88

    def3

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
91
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
5
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
83

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
0def3
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
96=__
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
99
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
00

def2

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
02

len1

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
04

Phương pháp 4: Sử dụng phương pháp hiểu danh sách

Python3

def

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
06

    Repeat(x):6

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
29____210
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
11
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
1212

def3

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
222==
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
25
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
83

    

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
16=

    len1

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
65

Phương pháp 5: Sử dụng phương pháp từ điển danh sách (không có chức năng đếm inbuild nào)

Python3

def

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
06

    

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
69=
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
71

    

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
5
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
6
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
7
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
76

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
0def3 Repeat(x):0
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
6
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
7
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
82

def2

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
84=
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
5

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
0
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
88
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
83

def2

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
84
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
4=
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
5

    

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
5
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
97
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
7
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
99

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
0def3 def02
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
5____
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
83

def2def06

    Repeat(x):6 def09

def3

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
222==
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
25
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
83

    

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
16=

    len1

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
65

Phương pháp 5: Sử dụng phương pháp từ điển danh sách (không có chức năng đếm inbuild nào)

Python3

def

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
06

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
14=
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
3

def93=

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
3

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
5
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
6
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
7 def99

    def3

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
6Repeat(x):0
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
7 Repeat(x):05

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
0Repeat(x):07

Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
5
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
6
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
7 Repeat(x):05

    def3 Repeat(x):14

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
5
Input :  list = [-1, 1, -1, 8]
Output : output_list = [-1]
83

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
0Repeat(x):18

len1Repeat(x):20

Phương pháp 7: Sử dụng chức năng liệt kê

Python3

Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
16______

Độ phức tạp về thời gian: O (n)O(n)

Không gian phụ trợ: O (1) O(1)


Làm thế nào để bạn kiểm tra các số trùng lặp trong Python?

ALGORITHM:..
Bước 1: Khai báo và khởi tạo một mảng ..
Bước 2: Có thể tìm thấy các phần tử trùng lặp bằng hai vòng. Vòng ngoài sẽ lặp qua mảng từ 0 đến chiều dài của mảng. ....
Bước 3: Nếu tìm thấy một trận đấu có nghĩa là phần tử trùng lặp được tìm thấy sau đó, hãy hiển thị phần tử ..

Làm cách nào để tìm thấy số trùng lặp?

Tìm và loại bỏ các bản sao..
Chọn các ô bạn muốn kiểm tra các bản sao. ....
Nhấp vào nhà> Định dạng có điều kiện> Đánh dấu các quy tắc ô> Các giá trị trùng lặp ..
Trong hộp bên cạnh các giá trị với, chọn định dạng bạn muốn áp dụng cho các giá trị trùng lặp, sau đó bấm OK ..

Làm thế nào để bạn tìm thấy số lượng các giá trị lặp lại trong một danh sách trong Python?

Toán tử.countof () được sử dụng để đếm số lần xuất hiện của B trong a.Nó đếm số lần xuất hiện của giá trị.Nó trả về số lượng của một số lần xuất hiện của giá trị. countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. It returns the Count of a number of occurrences of value.

Có một chức năng trùng lặp trong Python?

Hàm trùng lặp () cho biết các giá trị chuỗi trùng lặp.Các giá trị trùng lặp được chỉ định là giá trị thực trong chuỗi kết quả.Hoặc là tất cả các bản sao, tất cả ngoại trừ lần đầu tiên hoặc tất cả ngoại trừ sự xuất hiện cuối cùng của các bản sao có thể được chỉ định.. The duplicated values are indicated as True values in the resulting Series. Either all duplicates, all except the first or all except the last occurrence of duplicates can be indicated.