I came here in hopes that counting occurrences would be an easy solution to what I wanted to do, which is find if any duplicates exists. (More about this below.) I tried to find a solution without linq or multiple loops.
The code:
using System;
using System.Collections.Generic;
List<string> Words = new List<string>();
List<int> Occurrences = new List<int>();
// ~170 elements added...
// Note: Occurrences is still empty at this point
string[] Done = new string[Words.Count];
for (int i = 0; i < Done.Length; i++)
{
if (Array.IndexOf(Done, Words[i]) is int f && f > -1)
{
for (int o = 0; o < Done.Length; o++)
{
int d = Array.IndexOf(Done[o..], Words[i]);
if (d == -1) { break; }
Occurrences[o+d] += 1;
o += d;
}
Occurrences.Add(Occurrences[f]);
}
else
{
Occurrences.Add(1);
}
Done[i] = Words[i];
}
// To write the result as seen in the question
for (int i = 0; i < Done.Length; i++)
{
Console.Write("{0} ({1}), ", Words[i], Occurrences[i]);
}
Explanation:
The core is the nested for loop that checks for all occurrences, using IndexOf
, and the range struct. Here we have:
Done
which is an array that lists all previously checked items from the list
o
which is the index of the for loop, indicating the last occurrence +1 (o++
), starting at 0
d
which is the index of the first duplicate of the current item in Done
- A command that increases
o
with d
, if found, so that the next IndexOf
search continues with the next occurrence.
- A command that increases the occurrence count at index
o
+d
by 1. We need to add o
to d
, because d
is the index starting at o
(range).
- A break command to end the loop if no more occurrences are found.
Up in the hierarchy, there is a check, whether there are any previous duplicates of the current item in Done
, so that we can add 1 to the occurrences list, if there are no previous occurrences. Here, we have:
f
which is the index of the first occurrence found in Done
- If not found, the value is -1, otherwise bigger (index). This is implemented in the if/else expression.
- If found, in addition to increasing the count value of all previous occurrences, as stated above, the same value (total current occurrences) is also added to the occurrence list.
- If not found, a value of 1 (1 occurrence, the current item) is added to the list.
Finally, the main loop, which is the loop through all items in the Words
list.
- Each word has an index
i
that corresponds to the Done
array index, because the array has the same size.
- At the last step of the loop, the item is added to the
Done
array, so it can now be found as a duplicate, and its count can be updated.
Notes:
- This will include the occurrence (count) of each word accurately, but all, even duplicates.
- It would be better to remove duplicate occurrence in the count results, as other solutions do it (distinct). Such an implementation would make the code simpler, since only one value has to be increased, so instead of the inner loop, a single
IndexOf
would be enough. Just be sure to also add the items to an item list, so that this new list matches the reduced count list (or use a dictionary or list with value and count property for each item).
- Depending on duplicate occurrences, this is arguably worse than two loops, where the first one gives a distinct, shorter list, where each item then is counted in a second loop. I'm not one who has much insight into this, however.
As a bonus, here is what I took as a base, which is to find if any duplicates exist.
I have a class with a Clash
property and a separate variable that checks if any clashes exist. My class has a Number
property, which I want to check for duplicates. Here's what I came up with:
// My list to check for duplicates, obviously added some items to it
List <MyClass> MyList = new List <MyClass> ();
// My variable that indicates whether any duplicates exist
bool Clash;
Clash = false;
string[] Check = new string[MyList.Count];
for (int i = 0; i < Check.Length; i++)
{
if (MyList[i] is MyClass MC && MC.Number is string N)
{
MC.Clash = Array.IndexOf(Check, N) is int d && d > -1;
MyList[d].Clash = MC.Clash;
Clash = MC.Clash || Clash;
Check[i] = N;
}
}
I don't need the inner for
loop, because the initial list itself has the clash property that I want to update. If the list had a count (int
) item property, instead of clash, we could use a similar code for the count purpose as well.