Giải trình
Trong chương trình này, chúng ta cần in các yếu tố trùng lặp có trong mảng. Điều này có thể được thực hiện thông qua hai vòng. Vòng lặp đầu tiên sẽ chọn một phần tử và vòng lặp thứ hai sẽ lặp qua mảng bằng cách so sánh phần tử đã chọn với các phần tử khác. Nếu một trận đấu được tìm thấy, in phần tử trùng lặp.
Trong mảng trên, bản sao đầu tiên sẽ được tìm thấy tại INDEX 4, đó là bản sao của phần tử (2) có mặt tại INDEX 1. Vì vậy, các phần tử sao chép trong mảng trên là 2, 3 và 8.
Thuật toán
- Tuyên bố và khởi tạo một mảng.
- Các yếu tố trùng lặp có thể được tìm thấy 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. Vòng lặp bên ngoài sẽ chọn một phần tử. Vòng bên trong sẽ được sử dụng để so sánh phần tử đã chọn với phần còn lại của các phần tử của mảng.
- Nếu một trận đấu được tìm thấy 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ử.
Dung dịch
Python
Output:
Duplicate elements in given array: 2 3 8
C
Output:
Duplicate elements in given array: 2 3 8
Java
Output:
Duplicate elements in given array: 2 3 8
C#
Output:
Duplicate elements in given array: 2 3 8
PHP
Output:
Duplicate elements in given array: 2 3 8
Chủ đề tiếp theo##
Đư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):
____10Duplicate elements in given array: 2 3 8 1Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 3Duplicate elements in given array: 2 3 8 4
Duplicate elements in given array: 2 3 8 0Duplicate elements in given array: 2 3 8 6Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 8
Duplicate elements in given array: 2 3 8 0__ Duplicate elements in given array: 2 3 8 1Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 3Duplicate elements in given array: 2 3 8 4
Duplicate elements in given array: 2 3 8 5Duplicate elements in given array: 2 3 8 6Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 1Duplicate elements in given array: 2 3 8 9 Duplicate elements in given array: 2 3 8 0
Duplicate elements in given array: 2 3 8 5Duplicate elements in given array: 2 3 8 0 Duplicate elements in given array: 2 3 8 3Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 3Duplicate elements in given array: 2 3 8 6
Duplicate elements in given array: 2 3 8 7Duplicate elements in given array: 2 3 8 8 Duplicate elements in given array: 2 3 8 9Duplicate elements in given array: 2 3 8 2Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 2Duplicate elements in given array: 2 3 8 3
Duplicate elements in given array: 2 3 8 8Duplicate elements in given array: 2 3 8 9
Duplicate elements in given array: 2 3 8 0Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]1 Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]2
Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]3Duplicate elements in given array: 2 3 8 2 Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]55____56Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]8Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7Input : list = [-1, 1, -1, 8] Output : output_list = [-1]0Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7____58Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]8____57____Input : list = [-1, 1, -1, 8] Output : output_list = [-1]0Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]8
Các
def6 def7
Phương pháp 2: Sử dụng một vòng duy nhất cho vòng lặp
Python3
E
Duplicate elements in given array: 2 3 8 23Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 8
Duplicate elements in given array: 2 3 8 26Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 8
Duplicate elements in given array: 2 3 8 0 Duplicate elements in given array: 2 3 8 1Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 32
Duplicate elements in given array: 2 3 8 0____38 Duplicate elements in given array: 2 3 8 1Duplicate elements in given array: 2 3 8 5 Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 73
Duplicate elements in given array: 2 3 8 5Duplicate elements in given array: 2 3 8 40
Duplicate elements in given array: 2 3 8 5Duplicate elements in given array: 2 3 8 75
Duplicate elements in given array: 2 3 8 5Duplicate elements in given array: 2 3 8 48
def6Duplicate elements in given array: 2 3 8 50
Duplicate elements in given array: 2 3 8 0 Duplicate elements in given array: 2 3 8 1Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 73 Using Counter() function from collection module
Python3
____10Duplicate elements in given array: 2 3 8 8 Duplicate elements in given array: 2 3 8 82Duplicate elements in given array: 2 3 8 0Duplicate elements in given array: 2 3 8 51
Phương pháp 8: Sử dụng chức năng liệt kê
Duplicate elements in given array: 2 3 8 84___
def6Duplicate elements in given array: 2 3 8 94
def6Duplicate elements in given array: 2 3 8 78Duplicate elements in given array: 2 3 8 97Duplicate elements in given array: 2 3 8 78__
Độ phức tạp về thời gian: O (n)
Không gian phụ trợ: O (1)
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1}) [1, 2, 5, 9]Đư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.
Python3
Phương pháp 1: Sử dụng cách tiếp cận vũ lực
Duplicate elements in given array: 2 3 8 44Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 46
Duplicate elements in given array: 2 3 8 0 Duplicate elements in given array: 2 3 8 48Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 97Duplicate elements in given array: 2 3 8 51
____10Duplicate elements in given array: 2 3 8 53Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 97Duplicate elements in given array: 2 3 8 56
____10Duplicate elements in given array: 2 3 8 8 Duplicate elements in given array: 2 3 8 59Duplicate elements in given array: 2 3 8 0Duplicate elements in given array: 2 3 8 51
Duplicate elements in given array: 2 3 8 5Duplicate elements in given array: 2 3 8 8 Duplicate elements in given array: 2 3 8 64Duplicate elements in given array: 2 3 8 2Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 67Duplicate elements in given array: 2 3 8 68
Duplicate elements in given array: 2 3 8 7Duplicate elements in given array: 2 3 8 70
def6Duplicate elements in given array: 2 3 8 72
Phương pháp 5: Sử dụng phương pháp hiểu danh sách
Python3
def Duplicate elements in given array: 2 3 8 74
Is
Duplicate elements in given array: 2 3 8 8 Duplicate elements in given array: 2 3 8 90Duplicate elements in given array: 2 3 8 2Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 93Duplicate elements in given array: 2 3 8 51
Duplicate elements in given array: 2 3 8 0Duplicate elements in given array: 2 3 8 84Duplicate elements in given array: 2 3 8 2 Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]5____________Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7Repeat(x):3Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7Duplicate elements in given array: 2 3 8 0Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7__
Duplicate elements in given array: 2 3 8 0def6Duplicate elements in given array: 2 3 8 33
Phương pháp 6: 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 Duplicate elements in given array: 2 3 8 74
Is
Duplicate elements in given array: 2 3 8 8 Duplicate elements in given array: 2 3 8 90Duplicate elements in given array: 2 3 8 2Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 93Duplicate elements in given array: 2 3 8 51
Duplicate elements in given array: 2 3 8 0Duplicate elements in given array: 2 3 8 84Duplicate elements in given array: 2 3 8 2 Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]5____________Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7Repeat(x):3Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7Duplicate elements in given array: 2 3 8 0Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7__
Phương pháp 6: Sử dụng phương pháp từ điển danh sách (không có chức năng đếm inbuild nào)
Duplicate elements in given array: 2 3 8 5Duplicate elements in given array: 2 3 8 56Duplicate elements in given array: 2 3 8 51
____10Duplicate elements in given array: 2 3 8 37Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 39
Duplicate elements in given array: 2 3 8 0__ Duplicate elements in given array: 2 3 8 1Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 44
Duplicate elements in given array: 2 3 8 5Duplicate elements in given array: 2 3 8 8 Duplicate elements in given array: 2 3 8 5 Duplicate elements in given array: 2 3 8 1Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 50
Duplicate elements in given array: 2 3 8 7Duplicate elements in given array: 2 3 8 522 Duplicate elements in given array: 2 3 8 0
Duplicate elements in given array: 2 3 8 7Duplicate elements in given array: 2 3 8 522929Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 0
Duplicate elements in given array: 2 3 8 8 Duplicate elements in given array: 2 3 8 90Duplicate elements in given array: 2 3 8 2Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 93Duplicate elements in given array: 2 3 8 51
Duplicate elements in given array: 2 3 8 0Duplicate elements in given array: 2 3 8 84Duplicate elements in given array: 2 3 8 2 Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]5____________Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7Repeat(x):3Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7Duplicate elements in given array: 2 3 8 0Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]7__
Duplicate elements in given array: 2 3 8 0def6Duplicate elements in given array: 2 3 8 33
Phương pháp 6: 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
____10Duplicate elements in given array: 2 3 8 37Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 39
Duplicate elements in given array: 2 3 8 0__ Duplicate elements in given array: 2 3 8 1Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 44
Duplicate elements in given array: 2 3 8 5Duplicate elements in given array: 2 3 8 8 Duplicate elements in given array: 2 3 8 5 Duplicate elements in given array: 2 3 8 1Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 50
Duplicate elements in given array: 2 3 8 7Duplicate elements in given array: 2 3 8 522 Duplicate elements in given array: 2 3 8 0
Duplicate elements in given array: 2 3 8 7Duplicate elements in given array: 2 3 8 522929Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 0
Duplicate elements in given array: 2 3 8 0__ Duplicate elements in given array: 2 3 8 65Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 67
Duplicate elements in given array: 2 3 8 5Duplicate elements in given array: 2 3 8 8 Duplicate elements in given array: 2 3 8 70Duplicate elements in given array: 2 3 8 0Duplicate elements in given array: 2 3 8 51
Duplicate elements in given array: 2 3 8 7Duplicate elements in given array: 2 3 8 74
Duplicate elements in given array: 2 3 8 5Duplicate elements in given array: 2 3 8 86
def6Duplicate elements in given array: 2 3 8 88
Duplicate elements in given array: 2 3 8 0Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]1 Duplicate elements in given array: 2 3 8 77
Python3
Phương pháp 7: Sử dụng trong, không trong toán tử và phương thức đếm ()
E
Duplicate elements in given array: 2 3 8 82Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 8O(n)
Duplicate elements in given array: 2 3 8 61Duplicate elements in given array: 2 3 8 2 Duplicate elements in given array: 2 3 8 8 O(1)