Convert a double list to a grouped string The program has an input a list of doubles and the output needs to be a string containing the list values grouped by their value. The list values will be grouped if they are equal. Something like: input 9,77,5,5,31 => output 9 77 2*5 31

I created an algorithm in C# (in Java I think that is almost the same) for this but I am not sure if it can be improved regarding its speed or code quaility, or if it has some bugs that I could not see. The algorithm having also some more input, output examples is below.

    List&lt;double&gt; input = new List&lt;double&gt; { 11, 32, 32, 43}; // output 11 2*32 43
    //List&lt;double&gt; input = new List&lt;double&gt; { 11, 11, 43, 43 }; // output 2*11 2*43
    //List&lt;double&gt; input = new List&lt;double&gt; { 10, 11, 12, 13, 14, 15, 16 }; // output 10 11 12 13 14 15 16
    //List&lt;double&gt; input = new List&lt;double&gt; { 11, 11, 11, 11, 11 }; // output 5 * 11
    //List&lt;double&gt; input = new List&lt;double&gt; { 11, 11, 32, 22, 22, 22, 4, 10, 10 }; // output 2*11 32 3*22 4 2*10

    string listAsString = string.Empty;
    double nextElem = double.MinValue;
    for (int i = 0; i &lt; input.Count; i++)
    {
        double currentElem = input[i];

        if (i + 1 &lt; input.Count)
        {
            nextElem = input[i + 1];
        }

        int equalCount = 0;
        while (currentElem.Equals(nextElem) &amp;&amp; i &lt; input.Count)
        {
            equalCount++;
            i++;
            currentElem = nextElem;

            if (i &lt; input.Count)
            {
                nextElem = input[i];
            }
        }

        if (equalCount &lt; 2)
        {
            listAsString += currentElem + " ";
        }
        else
        {
            listAsString += equalCount + "*" + currentElem + " ";
            i--;
        }
    }

    Console.WriteLine(listAsString);


Please let me know if you noticed some bugs or see some improvements that can be done. 

Also if you know another implementation of this requirement please add it so that a comparation regarding results, speed, code quality between the algorithms can be done... and find the best way to handle this.
Disclaimer: Edit
OP later specified that the numbers must be grouped only if they are adjacent. My code does not do that. It groups all equal elements. Please refer to @Xiaoy312's answer for that. I have my own solution, but as that is not a significant improvement over @Xiaoy312's I'm not posting it. To me, @Xiaoy312's answer is overkill for the specific use case the OP mentions, it promotes good programming practices and code reusability.
Old answer
What you're trying to achieve here is similar to Run-Length Encoding, a basic data compression technique.
Off the bat, I'll make a few observations:

You are repeatedly concatenating immutable strings. Usually not a great idea, as it involves large overheads in creating and discarding a whole lot of string objects. Use StringBuilder in the System.Text namespace instead, and append to it instead of doing repeated string concatenation.

Too much code in one function. Split it up! An obvious candidate is the part which counts the number of occurrences of a value in the list. Try to follow separation of concerns and the Single Responsibility Principle.

You do redundant counting for values. For a particular value, you should cache its count and not repeat the count for the same value again (memoization).

Try LINQ. It might even make this code into a one-liner. And be more idiomatic at the same time, if a bit slower.

You could do this better by doing the logic in 2 passes - in the first, you construct a map between the unique values in the list and their count, and in the second, you use this map to generate the string representation.

Ideally for comparing 2 doubles for equality, keeping in mind their error margins and inexact representation of certain values, you would check that the modulus of the difference of the 2 values was less than some small epsilon. However, for code like this, that's probably overkill.

Try to program as generic as possible. Use an IEnumerable&lt;T&gt; instead of a very specific List&lt;double&gt;, you will be able to use the resulting code for many different types of collections. This also involves typing to interfaces instead of concrete classes.


Using most of the above suggestions, herein follows the code (test fiddle here):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class RLEofList
{
    
    private static Dictionary&lt;double, int&gt; CountMap(List&lt;double&gt; input)
    {
        var countMap = new Dictionary&lt;double, int&gt;(input.Count);
        foreach(double item in input)
        {
            if(!countMap.ContainsKey(item))
            {
                countMap.Add(item, input.Count(x =&gt; x == item));
            }
        }
        return countMap;
    }

    public static string ListToRLEString(List&lt;double&gt; input)
    {
        var countMap = CountMap(input);
        var accumulator = new StringBuilder(input.Count);//rough guess for capacity of StringBuilder
        foreach(KeyValuePair&lt;double, int&gt; itemWithCount in countMap)
        {
            if(itemWithCount.Value &gt; 1)
            {
                accumulator
                .Append(itemWithCount.Value)
                .Append(&quot;*&quot;);
            }
            accumulator
            .Append(itemWithCount.Key)
            .Append(&quot; &quot;);
        }
        return accumulator.ToString();
    }
    
    public static void Main()
    {
        var list = Console
            .ReadLine()
            .Split(',')
            .ToList()
            .ConvertAll&lt;double&gt;(x =&gt; Double.Parse(x));
        Console.WriteLine(ListToRLEString(list));
    }
}

The piece of code above will accept input as a single line of comma-separated numbers as per the example in the question. You might need to use Regex.Split for more complicated input patterns.
I'm not collapsing this code into a LINQ one-liner as I feel this is more educational.