Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get repeat elements in a list in c#

Tags:

c#

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<int> list = new List<int>() { 1, 3, 3, 6, 3, 6, 10 };
List<int> listIndex = new List<int>();
int yts = 0;

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

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;

        }
    }
}

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
like image 670
Reynaldo Delmonte Avatar asked Jul 26 '26 18:07

Reynaldo Delmonte


2 Answers

With LINQ, this becomes pretty easy:

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

The statement will create a Dictionary<int,int>, 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

like image 70
SomeBody Avatar answered Jul 29 '26 07:07

SomeBody


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

The error is in your for (int u = 0; u < 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 < list.Count; u++) loop to the following:

for (int u = 0; u < 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.)

like image 41
Matthew Watson Avatar answered Jul 29 '26 07:07

Matthew Watson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!