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
如何最快地调整这一阿雷拉元件,使之达到上述标准?
我的第一个解决办法是,如果最大数量的出现接近于阵列,那么就过于缓慢。 Length (1M)/ ValuesSpan(1000)
我尝试过这样的东西(这只是为了调和可能发生的情况,下限的解决办法几乎相同):
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;
}
}
}
}