这称为运行长度编码 (RLE),用于许多事物(例如 Windows 位图文件格式)以提供非常基本的压缩(尤其是如果原始文件包含大量重复值(例如位图或传真) ) 包含相同颜色的长期运行)。
int[] array = { ........ }; // your values...
for ( int i=0; i < array.Length; i++ )
{
int count = 1;
int value = array[i];
// Consume until different..
while ( i+1 < array.Length && array[i] == array[i+1] )
{
count++;
i++
}
Console.WriteLine("{0}:{1}", count, value);
}
// OR, as suggested by @jon [done in my head, so could probably be improved a lot...]
int count = 0;
int oldValue = -1;
for ( int i=0; i<array.Length; i++ )
{
int newValue = array[i];
count = ( newValue != oldValue ) ? 1 : count+1;
if ( i+1 >= array.Length || array[i+1] != newValue)
{
Console.WriteLine("{0}:{1}", count, newValue);
}
oldValue = newValue;
}