gpt4 book ai didi

c# - 使用哈希表的不同值

转载 作者:行者123 更新时间:2023-11-30 20:07:10 26 4
gpt4 key购买 nike

当循环遍历值或字段列表时,我正在执行不同的值。

Dictionary<string, int> _ddistinctValues = new Dictionary<string, int>();

foreach(string _word in _words) {
if(!_ddistinctValues.ContainsKey(_word)) {
_ddistinctValues.Add(_word, 1);
}
}

有没有更好的方法来做到这一点而不用保存“1”整数值来节省空间?我正在使用哈希表来更快地获取不同的值。也许散列没有值的词名,只是不同的键? C# 中是否有可以执行此操作的类?

这不是什么大问题。只是想知道。

谢谢。

最佳答案

只需使用 LINQ 并调用 Distinct() 在名单上。您可能希望将结果放入另一个列表。

var distinctWords = _words.Distinct().ToList();

这将在内部使用 HashSet<T> 以确定您感兴趣的集合的唯一性。

关于c# - 使用哈希表的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8774477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com