Solution with C# using the sort method of List
static void Main(string[] args)
{
List<int> list = new List<int> { 7, 4, 2, 3, 3, 5, 3, 11, 9, 2, 5 };
List<int> tmp = new List<int> { };
List<int> rep = new List<int> {};
//sorting the list
list.Sort();
for (var i = 0; i < list.Count(); i++)
{
int cantidad = list.LastIndexOf(list[i]) - list.IndexOf(list[i]);
if ( cantidad != 0)
{
//found repetitions
rep.AddRange(list.GetRange(list.IndexOf(list[i]), cantidad + 1));
i += cantidad;
}
else tmp.Add(list[i]);
}
tmp.AddRange(rep);
foreach (int data in tmp)
Console.WriteLine(data);
Console.ReadLine();
}
Solution with C# sorting out manually
static void Main(string[] args)
{
List<int> list = new List<int> { 7, 4, 2, 3, 3, 5, 3, 11, 9, 2 };
List<int> tmp = new List<int> {};
List<int> rep = new List<int> {};
foreach (int data in list)
{
if (tmp.Count() > 0)
{
int posicion = -1;
bool bandera = false;
for (var i=0; i < tmp.Count(); i++)
{
//searching a repetition
if (tmp[i] == data)
{
tmp.RemoveAt(i);
//adding to the repeated list at the end or in a determined position
for (var j = 0; j < rep.Count(); i++)
{
if (rep[j] > data)
{
bandera = true;
rep.Insert(j, data); //the one that was deleted
rep.Insert(j, data); //the one at the original list
break;
}
}
if (!bandera)
{
bandera = true;
rep.Add(data); //the one that was deleted
rep.Add(data); //the one at the original list
}
break;
}
//saving the position to be inserted
if (tmp[i] > data)
{
posicion = i;
break;
}
}
//was not a repetition
if (!bandera)
{
bool banderaRep = false;
//searching in the repeated list
for (var i = 0; i < rep.Count(); i++)
{
if (rep[i] == data)
{
banderaRep = true;
rep.Insert(i, data);
break;
}
}
//was not in the repeated list
if (!banderaRep)
{
if (posicion == -1)
tmp.Add(data);
else
tmp.Insert(posicion, data);
}
}
}
else
tmp.Add(data);
}
//join
tmp.AddRange(rep);
foreach (int data in tmp)
Console.WriteLine(data);
Console.ReadLine();
}
{7,4,5,11,9,3,3,2,2}? That is, does the order of the items matter, so long as the single are to the left and the repeated are to the right?