【问题标题】:Finding Duplicate String Arrays查找重复的字符串数组
【发布时间】:2016-07-08 18:39:33
【问题描述】:

我有一个很大的字符串数组列表,在这个List<string[]> 中可以有具有所有相同值(并且可能具有不同索引)的数组。我正在寻找并计算这些 duplicate 字符串数组,并有一个 Dictionary<string[], int>int 是计数(但是如果有比使用字典更好的方法,我有兴趣听听)。有人对如何实现这一目标有任何建议吗?非常感谢任何和所有输入,谢谢!

【问题讨论】:

  • 你能举例说明你想要达到的目标吗?
  • int 键应该存储什么?这不可能是计数,因为您可以拥有许多具有相同计数的数组
  • 请用示例数据详细说明您的问题。 Dictionary<int, string[]> 很混乱
  • @bryanmac 啊,是的,只是认为int 是计数,但Dictionary<string[], int> 会更有意义。感谢您指出这一点
  • @Saleem 谢谢我稍微修改了这个问题,并会尽快提供一个样本

标签: c# arrays linq list sorting


【解决方案1】:

您可以使用 linq GroupByIEqualityComparer 来比较 string[]

var items = new List<string[]>() 
    { 
        new []{"1", "2", "3" ,"4" }, 
        new []{"4","3", "2", "1"},
        new []{"1", "2"}
    };

var results = items
        .GroupBy(i => i, new UnorderedEnumerableComparer<string>())
        .ToDictionary(g => g.Key, g => g.Count());

无序列表的IEqualityComparer

public class UnorderedEnumerableComparer<T> : IEqualityComparer<IEnumerable<T>>
{
    public bool Equals(IEnumerable<T> x, IEnumerable<T> y)
    {
        return x.OrderBy(i => i).SequenceEqual(y.OrderBy(i => i));
    }
    // Just the count of the array, 
    // it violates the rule of hash code but should be fine here
    public int GetHashCode(IEnumerable<T> obj)
    {
        return obj.Count();
    }
}

.Net Fiddle

【讨论】:

    【解决方案2】:

    如果您使用出现次数作为KeyDictionary,您可能会发现重复的键。我建议使用Dictionary&lt;string, int&gt;,其中键表示字符串,值表示出现次数。现在我们可以使用Linq 语句了。

    var results = items.SelectMany(item=>item)
                       .GroupBy(item=>item)
                       .ToDictionary(g=>g.Key, g=>g.Count()); 
    

    其他方法是使用LookUp,它允许将一组键映射到一个或多个值

    var lookup = items.SelectMany(item=>item)
                      .GroupBy(item=>item)
                      .ToLookup(c=>c.Count(), c=>c.Key);
    

    工作example

    【讨论】:

    • 嗯,我认为这只是对列表中所有数组的单个字符串进行计数和分组?它不需要比较字符串,但每个字符串数组和 group by / count 数组具有所有相同的字符串值
    • 在这种情况下,第二种方法(查找)应该对您有用。
    • 啊...现在我明白了你的意思,你想按数组分组并计算该数组中的重复项,对吗?
    • 不,我认为您的第二种方法是我正在寻找的,只是遇到了 SelectMany 的问题。我想是因为我实际上在object 中有string[]Error CS0411: The type arguments for method System.Linq.Enumerable.SelectMany&lt;TSource,TResult&gt;(this System.Collections.Generic.IEnumerable&lt;TSource&gt;, System.Func&lt;TSource,System.Collections.Generic.IEnumerable&lt;TResult&gt;&gt;)' cannot be inferred from the usage. Try specifying the type arguments explicitly
    • 哦,我用第二种方法得到了相同的结果。不,每个数组中没有任何重复项 - 几乎希望在这样的场景中进行分组和计数:new [] { "camera", "lens", "tripod" } == new [] { "camera", "tripod", "lens" }
    【解决方案3】:
    import java.util.Scanner;
    public class Q1 {
    
    public static void main(String[] args) {
        System.out.println("String entry here --> ");
        Scanner input = new Scanner(System.in);
        String entry = input.nextLine();
        String[] words = entry.split("\\s");         
        System.out.println(words.length);
        for(int i=0; i<words.length; i++){
            int count = 0;
            if(words[i] != null){
                for(int j=i+1;j<words.length;j++){
                    if(words[j] != null){
                        if(words[i].equals(words[j])){
                            words[j] = null;
                            count++;
                        }
                    }
                    else{
                        continue;
                    }
                }
                if(count != 0){
                    System.out.println("Count of duplicate " + words[i] + " = " + count );
    
                }
            }
            else{
                continue;
            }
        }
        input.close();
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-03
      • 2018-01-21
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多