For example...
- I have an array of integers which is initialized by random values between 1 and 1000
- Array has 1M elements (it will probably have much more, but this is only the example)
- Number of each element's occurrences must be between 10 and 1010
What's the fastest way of adjusting this Array's elements so they would meet the mentioned criteria?
My first solution is simply too slow if max number of occurrences is close to array.Length (1M)/valuesSpan (1000)
I tried something like (This is only for aligning max of occurences, solution for the lower limit is almost the same):
Int64[] DistinctArrayElements = distinctArrayElements;
Dictionary<Int64, Int32> occurrences = new Dictionary<Int64, Int32>();
foreach (Int64 DistinctElement in DistinctArrayElements)
{
occurrences.Add(DistinctElement, 0);
}
foreach (Int64 ArrayElement in Arr)
{
occurrences[ArrayElement] += 1;
}
//I know this initialization can be done more nicely, so don't bother with this.
for (int j = 0; j < Arr.Length; j++)
{
if (occurrences[Arr[j]] > upperNoOfOccurrences)
{
for (int i = 0; i < Arr.Length; i++)
{
if (occurrences[Arr[i]] < upperNoOfOccurrences)
{
Arr[j] = Arr[i];
occurrences[Arr[i]] += 1;
occurrences[Arr[j]] -= 1;
}
}
}
}