Hướng dẫn find the element that appears twice in array python - tìm phần tử xuất hiện hai lần trong python mảng
Mà không cần nhập chức năng khác Kiểm tra xem danh sách có chứa hai phần tử giống nhau không Show
F.E List = [3,4,5,3] vì 3 lần xuất hiện hai lần trong danh sách trả về đúng Cảm ơn đã giúp đỡ Khi được hỏi ngày 6 tháng 5 năm 2020 lúc 21:31May 6, 2020 at 21:31
Bạn có thể chuyển đổi thành
Đã trả lời ngày 6 tháng 5 năm 2020 lúc 21:35May 6, 2020 at 21:35
a_guesta_guesta_guest 31.8k10 Huy hiệu vàng55 Huy hiệu bạc106 Huy hiệu Đồng10 gold badges55 silver badges106 bronze badges Nếu bạn cần kiểm tra xem phần tử có xảy ra chính xác hai lần không, bạn có thể làm điều này:
Đã trả lời ngày 6 tháng 5 năm 2020 lúc 21:39May 6, 2020 at 21:39
Ssharmassharmassharma 1511 Huy hiệu bạc3 Huy hiệu đồng1 silver badge3 bronze badges Có những cách tiếp cận khác nhau để giải quyết vấn đề này. Giải pháp 1: SỬ DỤNG SET ()
Giải pháp 2: Sử dụng từ điển băm
Trong giải pháp trên, chúng tôi thêm các yếu tố trong chế độ băm và liên tục kiểm tra xem nó có tồn tại ở đó không, nếu chúng tôi trả về đúng. Nếu danh sách không có bản sao hoặc nhiều hơn 2 yếu tố thì nó sẽ chỉ trả về sai. Giải pháp 3: Chúng tôi cũng có thể sử dụng quầy từ các bộ sưu tập
Đã trả lời ngày 6 tháng 5 năm 2020 lúc 22:00May 6, 2020 at 22:00 Đưa ra một loạt các số nguyên. Tất cả các số xảy ra hai lần ngoại trừ một số xảy ra một lần. Tìm số trong O (N) Thời gian & Không gian thêm không đổi. Ví dụ: & nbsp;
Cách tiếp cận (lực lượng vũ phu): Một giải pháp là kiểm tra mọi yếu tố nếu nó xuất hiện một lần hoặc không. Khi một phần tử có một lần xuất hiện duy nhất được tìm thấy, hãy trả lại nó. & NBSP;One solution is to check every element if it appears once or not. Once an element with a single occurrence is found, return it. Dưới đây là việc thực hiện phương pháp: C++ 0 1 2 3 4 5 4 7 4 9 0 1 2 3 4 5 6 4 8 6 2 3 4 3 4 5 6 7 8 4 0 6 0 6 5 5 4 7 8 6 0 1 0 1 7 Element occurring once is 25 0 4 Element occurring once is 28 0 1 4 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 72 1 4 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 75 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 76 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 77 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 76 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 79 1Element occurring once is 21 Element occurring once is 22 Element occurring once is 23 Element occurring once is 24 1 7 Element occurring once is 27 0Đầu ra Element occurring once is 2 Độ phức tạp thời gian của giải pháp này là không gian phụ trợ O (N2): O (1) làm không gian không đổi được sử dụng. Một giải pháp tốt hơn là sử dụng băm. & Nbsp;
Giải pháp này hoạt động trong thời gian O (N) nhưng yêu cầu thêm không gian. Giải pháp tốt nhất là sử dụng XOR. XOR của tất cả các phần tử mảng cung cấp cho chúng ta số với một lần xuất hiện. Ý tưởng này dựa trên hai sự thật sau. & NBSP;
Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 7 Dưới đây là việc triển khai thuật toán trên & nbsp; C++ 0 1 2 3 4 5 4 7 4 9 0 1 2 3 4 5 6 4 8 6set 9 6 2 3 4 3 0 4 Element occurring once is 28 0 1 4 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 72 1 4 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 75 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 76 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 77 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 76 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 79 1Element occurring once is 21 Element occurring once is 22 Element occurring once is 23 Element occurring once is 24 1 7 Element occurring once is 27 0Đầu ra 26 4 5 4 7 4 9 0 1 2 3 4 5 6 4 8 6set 9 6 2 3 4 3 0 4 Element occurring once is 28 0 1 4 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 72 1 4 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 75 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 76 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 77 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 76 Let us consider the above example. Let ^ be xor operator as in C and C++. res = 7 ^ 3 ^ 5 ^ 4 ^ 5 ^ 3 ^ 4 Since XOR is associative and commutative, above expression can be written as: res = 7 ^ (3 ^ 3) ^ (4 ^ 4) ^ (5 ^ 5) = 7 ^ 0 ^ 0 ^ 0 = 7 ^ 0 = 79 1 62 3 64 65 66 67 1 7 Element occurring once is 27 0Java 72 73 0 1 76 4 5 4 let arr[] = {7, 3, 5, 4, 5, 3, 4} Required no = 2*(sum_of_array_without_duplicates) - (sum_of_array) = 2*(7 + 3 + 5 + 4) - (7 + 3 + 5 + 4 + 5 + 3 + 4) = 2* 19 - 31 = 38 - 31 = 7 (required answer)6 4 9 1 0 6 4 87 88 89 6 2 3 4 94 95 96 4set 9 6 7 02 1 0 1 05 76 07 08 1 0Các 6 4 30 6 32Element occurring once is 22 34 35 36 37 38 1 0 0Python3 42 43 1 45 46 47 88 49Is 6 45 46 61 1 7 64Các 82 83 84 85 86C# 1 88 72 90 0 1 76 4 5 4 97 4 9 1 0 6 4 set 2 6 2 3 4 set 7 4set 9 6 7 02 1 0 1 05 76 07 08 1 0Các 6 4 30 6 32Element occurring once is 22 34 35 36 37 38 1 0 0if len(set( |