获取C#中列表中的重复元素。

huangapple go评论57阅读模式
英文:

Get repeat elements in a list in c#

问题

这是你的代码中的问题:

for (int u = 0; u < list.Count; u++)
{
    for (int j = 0; j < list.Count; j++)
    {
        if (list[u] == list[j])
        {
            int num = list[u];
            listIndex[num] = listIndex[num] + 1;
        }
    }
}

这个部分导致了不正确的结果,因为它重复计算了相同元素的次数多次。你可以使用以下代码替代它来获得正确的结果:

for (int u = 0; u < list.Count; u++)
{
    int num = list[u];
    listIndex[num] = listIndex[num] + 1;
}

这样,你将只对每个元素遍历一次,正确计算出每个元素在列表中出现的次数。

英文:

I know this code is not optimal but I need help with a question about it, I am trying to obtain the number of times each element of the list is repeated, I do this by increasing the number found in that index by 1 in the listIndexList, as a copy of the algorithm of ordering by counting, but I have a problem and that is that if the element is repeated more than 1, you automatically see the number that must represent the number of times it is repeated, it goes crazy and gives me very rare numbers, if it is repeated 2 times it says that it is 4 and if it is repeated 3 times it says that it is 9.

This is the code:

List&lt;int&gt; list = new List&lt;int&gt;() { 1, 3, 3, 6, 3, 6, 10 };
List&lt;int&gt; listIndex = new List&lt;int&gt;();
int yts = 0;

for (int i = 0; i &lt; list.Count; i++)
{
    if (list[i] &gt; yts)
    {
        yts = list[i];
    }
}
for(int i = 0; i &lt;= yts; i++)
{
    listIndex.Add(0);
}

for (int u = 0; u &lt; list.Count; u++)
{
    for (int j = 0; j &lt; list.Count; j++)
    {
        if (list[u] == list[j])

        {
            int num = list[u];
            listIndex[num] = listIndex[num] + 1;

        }
    }
}

foreach (int i in listIndex)
{
    Console.WriteLine(i);
}

this is what i need

listIndex[0] = 0
listIndex[1] = 1
listIndex[2] = 0
listIndex[3] = 3
listIndex[4] = 0
listIndex[5] = 0
listIndex[6] = 2
listIndex[7] = 0
listIndex[8] = 0
listIndex[9] = 0
listIndex[10] = 1

And this is what I receive:

listIndex[0] = 0
listIndex[1] = 1
listIndex[2] = 0
listIndex[3] = 9
listIndex[4] = 0
listIndex[5] = 0
listIndex[6] = 4
listIndex[7] = 0
listIndex[8] = 0
listIndex[9] = 0
listIndex[10] = 1

答案1

得分: 1

使用LINQ,这变得相当容易:

var result = list.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());

这个语句将创建一个Dictionary<int, int>,其中键是数字,值是列表中出现的次数。我们只需按列表的整数值进行分组,然后创建一个带有键和组计数的字典。

在线演示:https://dotnetfiddle.net/39qgWu

英文:

With LINQ, this becomes pretty easy:

var result = list.GroupBy(x =&gt; x).ToDictionary(x =&gt; x.Key, x =&gt; x.Count());

The statement will create a Dictionary&lt;int,int&gt;, where the key is the number and the value is the amount of occurences in the list. We can just group by the integer values of the list and then create a dictionary with the key and the count of the groups.

Online-demo: https://dotnetfiddle.net/39qgWu

答案2

得分: 1

错误出现在您的 for (int u = 0; u &lt; list.Count; u++) 循环中。

您已经初始化了 listIndex[] 列表,使其包含与 list 中的最大值加一相等的元素数量。这是正确的。

所以,现在您只需要遍历 list[] 中的值,并将每个值用作 listIndex[] 中的值的索引以进行递增。

因此,您只需要将您的 for (int u = 0; u &lt; list.Count; u++) 循环更改为以下内容:

for (int u = 0; u &lt; list.Count; u++)
{
    ++listIndex[list[u]];
}

请注意,我们正在使用 list[u] 作为索引进入 listIndex[],并递增该索引处的值。

(正如其他人指出的,如果 list[] 中的最大值较大,这种方法在内存使用方面效率低下,因为它将需要一个与该最大值相等的长度的列表。)

英文:

(I will just correct your original code rather than suggest alternative approaches)

The error is in your for (int u = 0; u &lt; list.Count; u++) loop.

You have already initialised the listIndex[] list so that it contains a number of elements equal to the maximum value in list plus one. This is correct.

So all you need to do now is to iterate through the values in list[] and use each of those values as the index of the value in listIndex[] to increment.

Thus, all you need to do is to change your for (int u = 0; u &lt; list.Count; u++) loop to the following:

for (int u = 0; u &lt; list.Count; u++)
{
    ++listIndex[list[u]];
}

Notice how we are using list[u] as an index into listIndex[] and we increment the value at that index.

(As others have pointed out, this approach is inefficient in its use of memory if the maximum value in list[] is large, because it will require a list with length equal to that maximum value.)

答案3

得分: 0

如果你必须在不使用 Linq 的情况下进行操作,那么在执行算法之前对列表进行排序会有很大帮助。这样,要查找的所有数字都会相邻,只需要一次迭代。类似这样的代码:

var list = new List<int>() { 1, 3, 3, 6, 3, 6, 10 };
list.Sort();

var listIndex = new List<int>() { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

int find = list[0];

for (int i = 1; i < list.Count; i++)
{
  int start = i - 1;
  while (list[i] == find && i < list.Count) i++;

  int count = i - start;

  listIndex[find] = count;

  find = list[i];
  listIndex[find] = 1;
}

希望对你有所帮助。

英文:

If you must do it without Linq, then sorting the list before the algorithm helps a lot. Then all numbers to find are next to each other and only one iteration is necessary. Something like this

    var list = new List&lt;int&gt;() { 1, 3, 3, 6, 3, 6, 10 };
    list.Sort();
    
    var listIndex = new List&lt;int&gt;() { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    
    int find = list[0];
    
    for (int i = 1; i &lt; list.Count; i++)
    {
      int start = i - 1;
      while (list[i] == find &amp;&amp; i &lt; list.Count) i++;
      
      int count = i - start;
      
      listIndex[find] = count;
      
      find = list[i];
      listIndex[find] = 1;
    }

huangapple
  • 本文由 发表于 2023年7月13日 12:03:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定