Question by
vitiet · May 26, 2020 at 08:25 AM ·
c#scripting problemlistoptimizationprogramming
A more optimized way to adjust duplicate integer values In a list?
Hi all,
I have a list of integers that is exposed in the inspector, and I need to check for duplicate and adjust them on validate to make sure that I won't be able to enter a duplicate value. From:
To:
I made this simple method to do just that, but it seems like it is getting close to O(n^3) or even more the higher the list count is.
private void AdjustDuplicateValues()
{
for (int index = 0; index < enumValues.Count; index++)
{
for (int i = 0; i < enumValues.Count; i++)
{
// check against all other element to see if duplicate
if (i != index && enumValues[index]== enumValues[i])
{
// increase the duplicate element by 1
enumValues[i]++;
// reset counters to recheck changed value
index = 0;
i = 0;
}
}
}
}
Is there any way to optimize this piece of code/or is there any better way to adjust integer values within a list?
before.png
(13.7 kB)
after.png
(17.1 kB)
Comment