如何在c#中找到给定字符串中的重复项

时间:2022-10-06 19:19:46

I want to find the duplicates for a given string, I tried for collections, It is working fine, but i don't know how to do it for a string.

我想找到给定字符串的重复项,我尝试了集合,它工作正常,但我不知道如何为字符串做。

Here is the code I tried for collections,

这是我为集合尝试的代码,

string name = "this is a a program program";
string[] arr = name.Split(' ');

var myList = new List<string>();
var duplicates = new List<string>();

foreach(string res in arr)
{
    if (!myList.Contains(res))
    {
        myList.Add(res);
    }
    else
    {
        duplicates.Add(res);
    }
}

foreach(string result in duplicates)
{
    Console.WriteLine(result);
}

Console.ReadLine();

But I want to find the duplicates for the below string and to store it in an array. How to do that?

但我想找到下面字符串的重复项并将其存储在数组中。怎么做?

eg:- string aa = "elements";

In the above string i want to find the duplicate characters and store it in an array

在上面的字符串中,我想找到重复的字符并将其存储在一个数组中

Can anyone help me?

谁能帮我?

7 个解决方案

#1


2  

        string name = "elements";
        var myList = new List<char>();
        var duplicates = new List<char>();

        foreach (char res in name)
        {
            if (!myList.Contains(res))
            {
                myList.Add(res);
            }
            else if (!duplicates.Contains(res))
            {
                duplicates.Add(res);
            }
        }

        foreach (char result in duplicates)
        {
            Console.WriteLine(result);
        }
        Console.ReadLine();

#2


3  

Linq solution:

Linq解决方案:

  string name = "this is a a program program";

  String[] result = name.Split(' ')
    .GroupBy(word => word)
    .Where(chunk => chunk.Count() > 1)
    .Select(chunk => chunk.Key)
    .ToArray();

  Console.Write(String.Join(Environment.NewLine, result));

The same princicple for duplicate characters within a string:

字符串中重复字符的相同原则:

 String source = "elements";

 Char[] result = source
   .GroupBy(c => c)
   .Where(chunk => chunk.Count() > 1)
   .Select(chunk => chunk.Key)
   .ToArray();

 // result = ['e']
 Console.Write(String.Join(Environment.NewLine, result));

#3


2  

string is an array of chars. So, you can use your collection approach. But, I would reccomend typed HashSet. Just load it with string and you'll get array of chars without duplicates, with preserved order.

string是一个字符数组。因此,您可以使用您的收集方法。但是,我会推荐键入HashSet。只需用字符串加载它就可以获得没有重复的字符数组,并保留顺序。

take a look:

看一看:

  string s = "aaabbcdaaee";
  HashSet<char> hash = new HashSet<char>(s);
  HashSet<char> hashDup = new HashSet<char>();
  foreach (var c in s)
    if (hash.Contains(c))
      hash.Remove(c);
    else
      hashDup.Add(c);
  foreach (var x in hashDup)
    Console.WriteLine(x);
  Console.ReadKey();

#4


1  

Instead of a List<> i'd use a HashSet<> because it doesn't allow duplicates and Add returns false in that case. It's more efficient. I'd also use a Dictionary<TKey,Tvalue> instead of the list to track the count of each char:

而不是List <>我使用HashSet <>因为它不允许重复,而在这种情况下Add返回false。它效率更高。我还使用Dictionary 而不是列表来跟踪每个char的计数: ,tvalue>

string text = "elements";
var duplicates = new HashSet<char>();
var duplicateCounts = new Dictionary<char, int>();

foreach (char c in text)
{
    int charCount = 0;
    bool isDuplicate = duplicateCounts.TryGetValue(c, out charCount);
    duplicateCounts[c] = ++charCount;
    if (isDuplicate)
        duplicates.Add(c);
} 

Now you have all unique duplicate chars in the HashSet and the count of each unique char in the dictionary. In this example the set only contains e because it's three times in the string.

现在,您在HashSet中拥有所有唯一的重复字符以及字典中每个唯一字符的计数。在此示例中,该集仅包含e,因为它在字符串中是三次。

So you could output it in the following way:

所以你可以用以下方式输出它:

foreach(char dup in duplicates)
    Console.WriteLine("Duplicate char {0} appears {1} times in the text."
                     , dup
                     , duplicateCounts[dup]);

For what it's worth, here's a LINQ one-liner which also creates a Dictionary that only contains the duplicate chars and their count:

对于它的价值,这里是一个LINQ单行程序,它还创建一个仅包含重复字符及其计数的字典:

Dictionary<char, int> duplicateCounts = text
            .GroupBy(c => c)
            .Where(g => g.Count() > 1)
            .ToDictionary(g => g.Key, g => g.Count());

I've shown it as second approach because you should first understand the standard way.

我已经把它作为第二种方法展示出来,因为你应该先了解标准方法。

#5


0  

        string name = "this is a a program program";
        var arr = name.Split(' ').ToArray();
        var dup = arr.Where(p => arr.Count(q => q == p) > 1).Select(p => p);
        HashSet<string> hash = new HashSet<string>(dup);
        string duplicate = string.Join(" ", hash);

#6


0  

You can do this through `LINQ

你可以通过`LINQ来做到这一点

string name = "this is a a program program";
var d = name.Split(' ').GroupBy(x => x).Select(y => new { word = y.Key, Wordcount = y.Count() }).Where(z=>z.cou > 1).ToList();

#7


0  

Use LINQ to group values:

使用LINQ对值进行分组:

public static IEnumerable<T> GetDuplicates<T>(this IEnumerable<T> list)
{
  return list.GroupBy(item => item).SelectMany(group => group.Skip(1));
}

public static bool HasDuplicates<T>(this IEnumerable<T> list)
{
    return list.GetDuplicates().IsNotEmpty();
}

Then you use these extensions like this:

然后你使用这样的扩展:

var list = new List<string> { "a", "b", "b", "c" };
var duplicatedValues = list.GetDuplicates();

#1


2  

        string name = "elements";
        var myList = new List<char>();
        var duplicates = new List<char>();

        foreach (char res in name)
        {
            if (!myList.Contains(res))
            {
                myList.Add(res);
            }
            else if (!duplicates.Contains(res))
            {
                duplicates.Add(res);
            }
        }

        foreach (char result in duplicates)
        {
            Console.WriteLine(result);
        }
        Console.ReadLine();

#2


3  

Linq solution:

Linq解决方案:

  string name = "this is a a program program";

  String[] result = name.Split(' ')
    .GroupBy(word => word)
    .Where(chunk => chunk.Count() > 1)
    .Select(chunk => chunk.Key)
    .ToArray();

  Console.Write(String.Join(Environment.NewLine, result));

The same princicple for duplicate characters within a string:

字符串中重复字符的相同原则:

 String source = "elements";

 Char[] result = source
   .GroupBy(c => c)
   .Where(chunk => chunk.Count() > 1)
   .Select(chunk => chunk.Key)
   .ToArray();

 // result = ['e']
 Console.Write(String.Join(Environment.NewLine, result));

#3


2  

string is an array of chars. So, you can use your collection approach. But, I would reccomend typed HashSet. Just load it with string and you'll get array of chars without duplicates, with preserved order.

string是一个字符数组。因此,您可以使用您的收集方法。但是,我会推荐键入HashSet。只需用字符串加载它就可以获得没有重复的字符数组,并保留顺序。

take a look:

看一看:

  string s = "aaabbcdaaee";
  HashSet<char> hash = new HashSet<char>(s);
  HashSet<char> hashDup = new HashSet<char>();
  foreach (var c in s)
    if (hash.Contains(c))
      hash.Remove(c);
    else
      hashDup.Add(c);
  foreach (var x in hashDup)
    Console.WriteLine(x);
  Console.ReadKey();

#4


1  

Instead of a List<> i'd use a HashSet<> because it doesn't allow duplicates and Add returns false in that case. It's more efficient. I'd also use a Dictionary<TKey,Tvalue> instead of the list to track the count of each char:

而不是List <>我使用HashSet <>因为它不允许重复,而在这种情况下Add返回false。它效率更高。我还使用Dictionary 而不是列表来跟踪每个char的计数: ,tvalue>

string text = "elements";
var duplicates = new HashSet<char>();
var duplicateCounts = new Dictionary<char, int>();

foreach (char c in text)
{
    int charCount = 0;
    bool isDuplicate = duplicateCounts.TryGetValue(c, out charCount);
    duplicateCounts[c] = ++charCount;
    if (isDuplicate)
        duplicates.Add(c);
} 

Now you have all unique duplicate chars in the HashSet and the count of each unique char in the dictionary. In this example the set only contains e because it's three times in the string.

现在,您在HashSet中拥有所有唯一的重复字符以及字典中每个唯一字符的计数。在此示例中,该集仅包含e,因为它在字符串中是三次。

So you could output it in the following way:

所以你可以用以下方式输出它:

foreach(char dup in duplicates)
    Console.WriteLine("Duplicate char {0} appears {1} times in the text."
                     , dup
                     , duplicateCounts[dup]);

For what it's worth, here's a LINQ one-liner which also creates a Dictionary that only contains the duplicate chars and their count:

对于它的价值,这里是一个LINQ单行程序,它还创建一个仅包含重复字符及其计数的字典:

Dictionary<char, int> duplicateCounts = text
            .GroupBy(c => c)
            .Where(g => g.Count() > 1)
            .ToDictionary(g => g.Key, g => g.Count());

I've shown it as second approach because you should first understand the standard way.

我已经把它作为第二种方法展示出来,因为你应该先了解标准方法。

#5


0  

        string name = "this is a a program program";
        var arr = name.Split(' ').ToArray();
        var dup = arr.Where(p => arr.Count(q => q == p) > 1).Select(p => p);
        HashSet<string> hash = new HashSet<string>(dup);
        string duplicate = string.Join(" ", hash);

#6


0  

You can do this through `LINQ

你可以通过`LINQ来做到这一点

string name = "this is a a program program";
var d = name.Split(' ').GroupBy(x => x).Select(y => new { word = y.Key, Wordcount = y.Count() }).Where(z=>z.cou > 1).ToList();

#7


0  

Use LINQ to group values:

使用LINQ对值进行分组:

public static IEnumerable<T> GetDuplicates<T>(this IEnumerable<T> list)
{
  return list.GroupBy(item => item).SelectMany(group => group.Skip(1));
}

public static bool HasDuplicates<T>(this IEnumerable<T> list)
{
    return list.GetDuplicates().IsNotEmpty();
}

Then you use these extensions like this:

然后你使用这样的扩展:

var list = new List<string> { "a", "b", "b", "c" };
var duplicatedValues = list.GetDuplicates();