Hướng dẫn write a python program to remove duplicates from the given string - viết một chương trình python để loại bỏ các bản sao khỏi chuỗi đã cho

Đưa ra một chuỗi s, nhiệm vụ là xóa tất cả các bản sao trong chuỗi đã cho. & NBSP; bên dưới là các phương thức khác nhau để loại bỏ các bản sao trong một chuỗi.S, the task is to remove all the duplicates in the given string. 
Below are the different methods to remove duplicates in a string.

Python

string=

  efgkors
0

  efgkors
1=
  efgkors
3

  efgkors
4
  efgkors
5
  efgkors
6
  efgkors
7

  efgkors
8
  efgkors
9
  efgkors
5
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
1
  efgkors
6
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
3

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
4
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
5

  efgkors
4
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
7
  efgkors
6
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
9
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
0
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
1
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
2
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
3
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
4

  efgkors
8
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
6
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
7=
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
9

& nbsp; phương pháp 1 (đơn giản) & nbsp;
METHOD 1 (Simple)  

Python3

string=

  efgkors
0

efgkors
3=
efgkors
5

  efgkors
4
efgkors
7
  efgkors
6
efgkors
9

  efgkors
8
  efgkors
9
efgkors
7
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
1
  efgkors
6
1: Initialize:
    str  =  "test string" /* input string */
    ip_ind =  0          /* index to  keep track of location of next
                             character in input string */
    res_ind  =  0         /* index to  keep track of location of
                            next character in the resultant string */
    bin_hash[0..255] = {0,0, ….} /* Binary hash to see if character is 
                                        already processed or not */
2: Do following for each character *(str + ip_ind) in input string:
              (a) if bin_hash is not set for *(str + ip_ind) then
                   // if program sees the character *(str + ip_ind) first time
                         (i)  Set bin_hash for *(str + ip_ind)
                         (ii)  Move *(str  + ip_ind) to the resultant string.
                              This is done in-place.
                         (iii) res_ind++
              (b) ip_ind++
  /* String obtained after this step is "the stringing" */
3: Remove extra characters at the end of the resultant string.
  /*  String obtained after this step is "te string" */
5

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
4
efgkors
3=
efgkors
3
geksfor
0
geksfor
1

Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
6
geksfor
3

geksfor
4=
geksfor
6
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
0
  efgkors
0
geksfor
9

Output:    

geksfor

Độ phức tạp về thời gian: o (n * n) & nbsp; không gian phụ trợ: o (1) & nbsp; giữ thứ tự của các phần tử giống như đầu vào. & Nbsp; O(n * n) 
Auxiliary Space : O(1) 
Keeps order of elements the same as input. 

Phương pháp 2 (sử dụng BST) & NBSP; sử dụng tập hợp thực hiện cây tìm kiếm nhị phân tự cân bằng. & NBSP; 
use set which implements a self-balancing Binary Search Tree. 

Python3

geksfor
0
geksfor
1______72

  efgkors
8
geksfor
5=
geksfor
7______

  efgkors
8
  efgkors
4
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
7
  efgkors
6
geksfor
2string4

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
4string6

  efgkors
8string8=
efgkors
5

  efgkors
8
  efgkors
4
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
7
  efgkors
6 =5

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
4string8= =9
geksfor
0
  efgkors
01

  efgkors
8
  efgkors
03 =9

geksfor
2 =
  efgkors
0

Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
6
  efgkors
15
geksfor
6
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
0
geksfor
2
  efgkors
19

Output:    

  efgkors

Độ phức tạp về thời gian: O (n log n) & nbsp; không gian phụ trợ: O (n): O(n Log n) 
Auxiliary Space: O(n)

Cảm ơn Anivesh Tiwari đã đề xuất phương pháp này.Anivesh Tiwari for suggesting this approach.

Nó không giữ thứ tự của các phần tử giống như đầu vào nhưng in chúng theo thứ tự được sắp xếp.

Phương pháp 3 (sử dụng sắp xếp) & nbsp; thuật toán: & nbsp; 
Algorithm: 

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.

Example:    

Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors

Lưu ý rằng, phương pháp này không giữ được thứ tự ban đầu của chuỗi đầu vào. Ví dụ: nếu chúng ta loại bỏ các bản sao cho GeekSforGeek và giữ thứ tự các ký tự giống nhau, thì đầu ra phải là Geksfor, nhưng hàm trên trả về EFGKO. Chúng tôi có thể sửa đổi phương pháp này bằng cách lưu trữ thứ tự ban đầu.

Implementation:    

Python

geksfor
0
  efgkors
21

  efgkors
8
  efgkors
23=
  efgkors
3

  efgkors
8
  efgkors
4
  efgkors
28
  efgkors
6
efgkors
9

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
4
  efgkors
32

  efgkors
8
  efgkors
03
  efgkors
35

geksfor
0
  efgkors
37
  efgkors
38
  efgkors
39

  efgkors
8
  efgkors
03
  efgkors
42
  efgkors
38
geksfor
9

geksfor
0
  efgkors
46
  efgkors
38
  efgkors
39

  efgkors
8
  efgkors
50__
  efgkors
52

  efgkors
8
  efgkors
54=
  efgkors
52

  efgkors
8
  efgkors
58
  efgkors
59____9
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
3
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
0
  efgkors
38
  efgkors
39

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
4
  efgkors
9
  efgkors
38
  efgkors
68=
  efgkors
38
  efgkors
71
  efgkors
72
  efgkors
522

  efgkors
75
  efgkors
38
  efgkors
777____9
  efgkors
38
  efgkors
80

  efgkors
75
  efgkors
50
geksfor
0=
  efgkors
52

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
4
  efgkors
87
geksfor
0=
  efgkors
52

  efgkors
8
  efgkors
92=
  efgkors
37
  efgkors
38
  efgkors
96
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
1
  efgkors
98

  efgkors
8
  efgkors
03 string

geksfor
0
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
03

  efgkors
8
  efgkors
38 =
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
07

  efgkors
8
  efgkors
38
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
10

  efgkors
8
  efgkors
03
  efgkors
46
  efgkors
38
geksfor
9

  efgkors
92=
  efgkors
0

Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
6
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
20

Output:    

efgkors

Độ phức tạp về thời gian: O (n log n) Nếu chúng ta sử dụng một số thuật toán sắp xếp nlogn thay vì QuickSort. O(n log n) If we use some nlogn sorting algorithm instead of quicksort.

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

Phương pháp 4 (sử dụng băm) & nbsp; 

Algorithm:    

1: Initialize:
    str  =  "test string" /* input string */
    ip_ind =  0          /* index to  keep track of location of next
                             character in input string */
    res_ind  =  0         /* index to  keep track of location of
                            next character in the resultant string */
    bin_hash[0..255] = {0,0, ….} /* Binary hash to see if character is 
                                        already processed or not */
2: Do following for each character *(str + ip_ind) in input string:
              (a) if bin_hash is not set for *(str + ip_ind) then
                   // if program sees the character *(str + ip_ind) first time
                         (i)  Set bin_hash for *(str + ip_ind)
                         (ii)  Move *(str  + ip_ind) to the resultant string.
                              This is done in-place.
                         (iii) res_ind++
              (b) ip_ind++
  /* String obtained after this step is "the stringing" */
3: Remove extra characters at the end of the resultant string.
  /*  String obtained after this step is "te string" */

Implementation:    

Python

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
21=
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
23

geksfor
0
  efgkors
21

  efgkors
8
  efgkors
23=
  efgkors
3

  efgkors
8
  efgkors
4
  efgkors
28
  efgkors
6
efgkors
9

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
4
  efgkors
38
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
37

  efgkors
8
  efgkors
03
  efgkors
35

geksfor
0
  efgkors
37
  efgkors
38
  efgkors
39

  efgkors
8
  efgkors
03
  efgkors
42
  efgkors
38
geksfor
9

geksfor
0
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
03

  efgkors
8
  efgkors
38 =
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
07

  efgkors
8
  efgkors
03
  efgkors
46
  efgkors
38
geksfor
9

  efgkors
92=
  efgkors
0

Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
6
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
20

efgkors

Độ phức tạp về thời gian: O (n log n) Nếu chúng ta sử dụng một số thuật toán sắp xếp nlogn thay vì QuickSort.

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

Phương pháp 4 (sử dụng băm) & nbsp;

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
21=
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
23

  efgkors
8
  efgkors
38 =
  efgkors
3

  efgkors
75
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
06
geksfor
0=
  efgkors
52

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
4
  efgkors
87
geksfor
0=
  efgkors
52

  efgkors
8
  efgkors
03
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
17
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
1
  efgkors
98

  efgkors
92=
  efgkors
0

Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
6
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
24

Output:    

geksfor

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

Điểm quan trọng: & nbsp; & nbsp;  

  • Phương pháp 2 không duy trì các ký tự như chuỗi gốc, nhưng Phương pháp 4 thì có.
  • Giả định rằng số lượng ký tự có thể trong chuỗi đầu vào là 256. NO_OF_CHARS nên được thay đổi tương ứng.
  • Calloc () được sử dụng thay vì malloc () để phân bổ bộ nhớ của một mảng đếm (đếm) để khởi tạo bộ nhớ được phân bổ thành ‘�. malloc () theo sau là memset () cũng có thể được sử dụng.
  • Thuật toán trên cũng hoạt động cho các đầu vào mảng số nguyên nếu phạm vi của các số nguyên trong mảng được đưa ra. Một vấn đề ví dụ là tìm số lượng xảy ra tối đa trong một mảng đầu vào cho rằng mảng đầu vào chỉ chứa các số nguyên chỉ trong khoảng từ 1000 đến 1100

Phương pháp 5 (sử dụng phương thức indexof ()): & nbsp; điều kiện tiên quyết: Java indexof () phương thức & nbsp; & nbsp; (Using IndexOf() method) : 
Prerequisite : Java IndexOf() method  

Python3

geksfor
0
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
26

  efgkors
8string8=
efgkors
5

  efgkors
8
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
32=
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
3
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
35

  efgkors
8
  efgkors
4
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
7
  efgkors
6
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
9
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
41

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
4
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
43=
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
45

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
4
  efgkors
9
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
43
  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.
1
  efgkors
6
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
51

  efgkors
75string8
geksfor
0=
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
56

  efgkors
8
  efgkors
03 =9

  efgkors
9
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
61=__
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
64string4

  efgkors
8
geksfor
5=
  efgkors
0

  efgkors
8
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
6
Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkorss
2) Remove duplicates
    efgkorskkorss
3) Remove extra characters
     efgkors
72

Đầu ra: & nbsp; & nbsp;   

geksfor

Cảm ơn Debjitdbb vì đã đề xuất phương pháp này. Xin vui lòng tham khảo bài viết hoàn chỉnh về Xóa các bản sao khỏi một chuỗi nhất định để biết thêm chi tiết! & NBSP;debjitdbb for suggesting this approach.
Please refer complete article on Remove duplicates from a given string for more details!
 


Làm thế nào để bạn loại bỏ các bản sao khỏi một chuỗi trong Python?

Dưới đây là các phương pháp khác nhau để loại bỏ các bản sao trong một chuỗi ...
Sắp xếp các yếu tố ..
Bây giờ trong một vòng lặp, loại bỏ các bản sao bằng cách so sánh ký tự hiện tại với ký tự trước đó ..
Xóa các ký tự bổ sung ở cuối chuỗi kết quả ..

Làm cách nào để loại bỏ tất cả các bản sao khỏi một chuỗi?

Chúng ta nên sử dụng các bước sau để loại bỏ các bản sao ...
Trong bước đầu tiên, chúng ta phải chuyển đổi chuỗi thành một mảng ký tự ..
Tính kích thước của mảng ..
Phương thức gọi lại sao chép () bằng cách chuyển mảng ký tự và độ dài ..
Traverse tất cả các ký tự có trong mảng ký tự ..

Làm thế nào để bạn tìm thấy các bản sao trong một chuỗi trăn?

Python..
chuỗi = "Trách nhiệm lớn" ;.
in ("ký tự trùng lặp trong một chuỗi đã cho:") ;.
#Count mỗi ký tự có trong chuỗi ..
cho i trong phạm vi (0, len (chuỗi)):.
Đếm = 1 ;.
cho J trong phạm vi (i+1, len (chuỗi)):.
if (chuỗi [i] == chuỗi [j] và chuỗi [i]! = ''):.
Đếm = đếm + 1 ;.

Làm thế nào để bạn loại bỏ các bản sao trong Python với các chuỗi liên tiếp?

Hủy bỏ tất cả các bản sao liên tiếp khỏi chuỗi bằng cách sử dụng đệ quy:..
Nếu chuỗi trống, hãy quay lại ..
Khác so sánh các ký tự liền kề của chuỗi.Nếu chúng giống nhau thì hãy thay đổi từng ký tự sang trái.Cuộc gọi đệ quy trên chuỗi s ..
Nếu chúng không giống nhau thì hãy gọi đệ quy từ chuỗi S+1 ..