There is a similar question posted, but I do not have the rep to ask a follow-up question in that thread. :(
If I have a List<T>
that contains items that appear more than once, List.Distinct()
will remove duplicates, but the original will still remain in place. If I want to remove items that occur more than once, including the original, what would be the most efficient way to do this to the original list?
Given a List<int>
called oneTime
:
{ 4, 5, 7, 3, 5, 4, 2, 4 }
The desired output would be in oneTime
:
{ 7, 3, 2 }
Follow up question for @Enigmativity:
Here is a pseudo version of what my script is doing. It is done in NinjaTrader which runs on .NET3.5.
I will attach a general idea of what the code is supposed to be doing, I'd attach the actual script but unless using NinjaTrader, it might not be of use.
But essentially, there is a large z loop. Each time through, a series of numbers is added to 'LiTics.' Which I do not want to disturb. I then pass that list to the function, and return a list of values that only occur once. Then I'd like to see those numbers each time through the loop.
It works initially, but running this on various sets of data, after a few passes through the loop, it start reporting values that occur more than once. I'm not sure why exactly?
for(int z=1; z<=10000; z +=1)//Runs many times
{
if (BarsInProgress ==0 &&CurrentBar-oBarTF1>0 &&startScript ) //Some Condition
{
for(double k=Low[0]; k<=High[0]; k +=TickSize)
{
LiTics.Add(k);
//Adds a series of numbers to this list each time through z loop
//This is original that I do not want to disturb
}
LiTZ.Clear(); //Display list to show me results Clear before populating
LiTZ=GetTZone(LiTics); //function created in thread(below)
//Passing the undisturbed list that is modified on every loop
foreach (double prime in LiTZ) { Print(Times[0] +", " +prime); }
//Printing to see results
}
}//End of bigger 'z' loop
//Function created to get values that appear ONLY once
public List<double> GetTZone(List<double> sequence)
{
var result =
sequence
.GroupBy(x => x)
.Where(x => !x.Skip(1).Any())
.Select(x => x.Key)
.ToList();
return result;
}
A picture of the print out and what is going wrong: Screenshot.