📌  相关文章
📜  根据最后出现的顺序从字符串删除重复项

📅  最后修改于: 2021-09-03 04:18:13             🧑  作者: Mango

给定一个字符串,从字符串中删除重复的字符,保留了重复的字符最后一次出现。假设字符区分大小写。
例子:

天真的解决方案:从左到右遍历给定的字符串。对于每个字符,检查它是否也出现在右侧。如果是,则不包含在输出中,否则包含它。

C++
// C++ program to remove duplicate character
// from character array and print in sorted
// order
#include 
using namespace std;
 
string removeDuplicates(string str)
{
    // Used as index in the modified string
    int n = str.length();
 
    // Traverse through all characters
    string res = "";
    for (int i = 0; i < n; i++) {
 
        // Check if str[i] is present before it
        int j;
        for (j = i+1; j < n; j++)
            if (str[i] == str[j])
                break;
 
        // If not present, then add it to
        // result.
        if (j == n)
            res = res + str[i];
    }
    return res;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    cout << removeDuplicates(str);
    return 0;
}


Java
// Java program to remove duplicate character
// from character array and print in sorted
// order
 
import java.util.*;
 
class GFG{
 
static String removeDuplicates(String str)
{
    // Used as index in the modified String
    int n = str.length();
 
    // Traverse through all characters
    String res = "";
    for (int i = 0; i < n; i++)
    {
        // Check if str[i] is present before it
        int j;
        for (j = i + 1; j < n; j++)
            if (str.charAt(i) == str.charAt(j))
                break;
 
        // If not present, then add it to
        // result.
        if (j == n)
            res = res + str.charAt(i);
    }
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    String str = "geeksforgeeks";
    System.out.print(removeDuplicates(str));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to remove duplicate character
# from character array and prin sorted
# order
 
def removeDuplicates(str):
      
    # Used as index in the modified string
    n = len(str)
 
    # Traverse through all characters
    res = ""
    for i in range(n):
 
        # Check if str[i] is present before it
        j = i + 1
        while j < n:
            if (str[i] == str[j]):
                break
            j += 1
 
        # If not present, then add it to
        # result.
        if (j == n):
            res = res + str[i]
    return res
 
# Driver code
if __name__ == '__main__':
    str = "geeksforgeeks"
    print(removeDuplicates(str))
 
# This code is contributed by mohit kumar 29


C#
// C# program to remove duplicate character
// from character array and print in sorted
// order
using System;
 
class GFG{
 
static String removeDuplicates(String str)
{
     
    // Used as index in the modified String
    int n = str.Length;
 
    // Traverse through all characters
    String res = "";
    for(int i = 0; i < n; i++)
    {
 
       // Check if str[i] is present before it
       int j;
       for(j = i + 1; j < n; j++)
          if (str[i] == str[j])
              break;
 
       // If not present, then add it to
       // result.
       if (j == n)
           res = res + str[i];
    }
     
    return res;
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "geeksforgeeks";
    Console.Write(removeDuplicates(str));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


C++
// C++ program to remove duplicate character
// from character array and print in sorted
// order
#include 
using namespace std;
 
string removeDuplicates(string str)
{
    // Used as index in the modified string
    int n = str.length();
 
    // Create an empty hash table
    unordered_set s;
 
    // Traverse through all characters from
    // right to left
    string res = "";
    for (int i = n-1; i >= 0; i--) {
 
       // If current character is not in
       if (s.find(str[i]) == s.end())
       {
          res = res + str[i];
          s.insert(str[i]);
       }
    }
 
    // Reverse the result string
    reverse(res.begin(), res.end());
 
    return res;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    cout << removeDuplicates(str);
    return 0;
}


Java
// Java program to remove duplicate character
// from character array and print in sorted
// order
import java.util.*;
 
class GFG{
 
static String removeDuplicates(String str)
{
     
    // Used as index in the modified String
    int n = str.length();
 
    // Create an empty hash table
    HashSet s = new HashSet();
 
    // Traverse through all characters from
    // right to left
    String res = "";
    for(int i = n - 1; i >= 0; i--)
    {
         
       // If current character is not in
       if (!s.contains(str.charAt(i)))
       {
           res = res + str.charAt(i);
           s.add(str.charAt(i));
       }
    }
     
    // Reverse the result String
    res = reverse(res);
    return res;
}
 
static String reverse(String input)
{
     
    char[] a = input.toCharArray();
    int l, r = a.length - 1;
     
    for(l = 0; l < r; l++, r--)
    {
       char temp = a[l];
       a[l] = a[r];
       a[r] = temp;
    }
    return String.valueOf(a);
}
 
// Driver code
public static void main(String[] args)
{
    String str = "geeksforgeeks";
    System.out.print(removeDuplicates(str));
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program to remove duplicate character
# from character array and prin sorted order
def removeDuplicates(str):
     
    # Used as index in the modified string
    n = len(str)
     
    # Create an empty hash table
    s = set()
     
    # Traverse through all characters from
    # right to left
    res = ""
    for i in range(n - 1, -1, -1):
         
        # If current character is not in
        if (str[i] not in s):
            res = res + str[i]
            s.add(str[i])
     
    # Reverse the result string
    res = res[::-1]
    return res
 
# Driver code
str = "geeksforgeeks"
 
print(removeDuplicates(str))
 
# This code is contributed by ShubhamCoder


C#
// C# program to remove duplicate character
// from character array and print in sorted
// order
using System;
using System.Collections.Generic;
 
class GFG{
 
static String removeDuplicates(String str)
{
     
    // Used as index in the modified String
    int n = str.Length;
 
    // Create an empty hash table
    HashSet s = new HashSet();
 
    // Traverse through all characters
    // from right to left
    String res = "";
    for(int i = n - 1; i >= 0; i--)
    {
         
        // If current character is not in
        if (!s.Contains(str[i]))
        {
            res = res + str[i];
            s.Add(str[i]);
        }
    }
     
    // Reverse the result String
    res = reverse(res);
    return res;
}
 
static String reverse(String input)
{
    char[] a = input.ToCharArray();
    int l, r = a.Length - 1;
     
    for(l = 0; l < r; l++, r--)
    {
        char temp = a[l];
        a[l] = a[r];
        a[r] = temp;
    }
    return String.Join("", a);
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "geeksforgeeks";
    Console.Write(removeDuplicates(str));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:

forgeks

时间复杂度:O(n*n)
有效的解决方案:这个想法是使用散列。
1)初始化一个空的hash表,res =“”
2) 从右到左遍历输入字符串。如果当前字符不在哈希表中,则将其附加到 res 并将其插入哈希表中。否则无视。

C++

// C++ program to remove duplicate character
// from character array and print in sorted
// order
#include 
using namespace std;
 
string removeDuplicates(string str)
{
    // Used as index in the modified string
    int n = str.length();
 
    // Create an empty hash table
    unordered_set s;
 
    // Traverse through all characters from
    // right to left
    string res = "";
    for (int i = n-1; i >= 0; i--) {
 
       // If current character is not in
       if (s.find(str[i]) == s.end())
       {
          res = res + str[i];
          s.insert(str[i]);
       }
    }
 
    // Reverse the result string
    reverse(res.begin(), res.end());
 
    return res;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    cout << removeDuplicates(str);
    return 0;
}

Java

// Java program to remove duplicate character
// from character array and print in sorted
// order
import java.util.*;
 
class GFG{
 
static String removeDuplicates(String str)
{
     
    // Used as index in the modified String
    int n = str.length();
 
    // Create an empty hash table
    HashSet s = new HashSet();
 
    // Traverse through all characters from
    // right to left
    String res = "";
    for(int i = n - 1; i >= 0; i--)
    {
         
       // If current character is not in
       if (!s.contains(str.charAt(i)))
       {
           res = res + str.charAt(i);
           s.add(str.charAt(i));
       }
    }
     
    // Reverse the result String
    res = reverse(res);
    return res;
}
 
static String reverse(String input)
{
     
    char[] a = input.toCharArray();
    int l, r = a.length - 1;
     
    for(l = 0; l < r; l++, r--)
    {
       char temp = a[l];
       a[l] = a[r];
       a[r] = temp;
    }
    return String.valueOf(a);
}
 
// Driver code
public static void main(String[] args)
{
    String str = "geeksforgeeks";
    System.out.print(removeDuplicates(str));
}
}
 
// This code is contributed by sapnasingh4991

蟒蛇3

# Python3 program to remove duplicate character
# from character array and prin sorted order
def removeDuplicates(str):
     
    # Used as index in the modified string
    n = len(str)
     
    # Create an empty hash table
    s = set()
     
    # Traverse through all characters from
    # right to left
    res = ""
    for i in range(n - 1, -1, -1):
         
        # If current character is not in
        if (str[i] not in s):
            res = res + str[i]
            s.add(str[i])
     
    # Reverse the result string
    res = res[::-1]
    return res
 
# Driver code
str = "geeksforgeeks"
 
print(removeDuplicates(str))
 
# This code is contributed by ShubhamCoder

C#

// C# program to remove duplicate character
// from character array and print in sorted
// order
using System;
using System.Collections.Generic;
 
class GFG{
 
static String removeDuplicates(String str)
{
     
    // Used as index in the modified String
    int n = str.Length;
 
    // Create an empty hash table
    HashSet s = new HashSet();
 
    // Traverse through all characters
    // from right to left
    String res = "";
    for(int i = n - 1; i >= 0; i--)
    {
         
        // If current character is not in
        if (!s.Contains(str[i]))
        {
            res = res + str[i];
            s.Add(str[i]);
        }
    }
     
    // Reverse the result String
    res = reverse(res);
    return res;
}
 
static String reverse(String input)
{
    char[] a = input.ToCharArray();
    int l, r = a.Length - 1;
     
    for(l = 0; l < r; l++, r--)
    {
        char temp = a[l];
        a[l] = a[r];
        a[r] = temp;
    }
    return String.Join("", a);
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "geeksforgeeks";
    Console.Write(removeDuplicates(str));
}
}
 
// This code is contributed by Rajput-Ji

Javascript


输出:
forgeks

时间复杂度:O(n)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live