David
David

Reputation: 2563

LINQ: Separating single list to multiple lists

I have a single array with these entries:

{1, 1, 2, 2, 3,3,3, 4}

and i want to transform them to ( 3 lists in this case ):

{1,2,3,4}
{1,2,3}
{3}

Is there any way to do this with LINQ or SQL? I guess there's a mathematical term for this operation, which I don't know unfortunately...

Or do I have to do it with loops?

=======

EDIT: I can't really describe the logic, so here are more examples.. It more or less loops multiple times over the array and takes every number once ( but every number only once per round ) until there are no numbers left

{1, 1, 2, 2, 3,3,3, 4, 5} would be {1,2,3,4,5} {1,2,3} {3}

or

{1, 1, 2, 2,2, 3,3,3, 4, 5} would be {1,2,3,4,5} {1,2,3} {2,3}

Upvotes: 3

Views: 231

Answers (3)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236248

private IEnumerable<List<int>> FooSplit(IEnumerable<int> items)
{
    List<int> source = new List<int>(items);
    while (source.Any())
    {
        var result = source.Distinct().ToList();
        yield return result;
        result.ForEach(item => source.Remove(item));
    }
}

Usage:

int[] items =  { 1, 1, 2, 2, 3, 3, 3, 4 };

foreach(var subList in FooSplit(items))
{
    // here you have your three sublists
}

Here is another solution, which is less readable but it will have better performance:

private IEnumerable<IEnumerable<int>> FooSplit(IEnumerable<int> items)
{
    var groups = items.GroupBy(i => i).Select(g => g.ToList()).ToList();    

    while (groups.Count > 0)
    {
       yield return groups.Select( g => 
             { var i = g[0]; g.RemoveAt(g.Count - 1); return i; });
       groups.RemoveAll(g => g.Count == 0);
    }
}

Upvotes: 10

Roland Mai
Roland Mai

Reputation: 31077

Here's an alternative console app:

class Program
{
    class Freq
    {
        public int Num { get; set; }
        public int Count { get; set; }
    }

    static void Main(string[] args)
    {
        var nums = new[] { 1, 1, 2, 2, 3, 3, 3, 4 };
        var groups = nums.GroupBy(i => i).Select(g => new Freq { Num = g.Key, Count = g.Count() }).ToList();
        while (groups.Any(g => g.Count > 0))
        {
            var list = groups.Where(g => g.Count > 0).Select(g => g.Num).ToList();
            list.ForEach(li => groups.First(g => g.Num == li).Count--);
            Console.WriteLine(String.Join(",", list));
        }

        Console.ReadKey();
    }

}

Upvotes: 1

Parimal Raj
Parimal Raj

Reputation: 20585

this does the job:

    static void Main(string[] args)
    {
        int[] numbers = {1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 5};
        List<int> nums = new List<int>(numbers.Length);
        nums.AddRange(numbers);

        while (nums.Count > 0)
        {
            int[] n = nums.Distinct().ToArray();
            for (int i = 0; i < n.Count(); i++)
            {
                Console.Write("{0}\t", n[i]);
                nums.Remove(n[i]);
            }
            Console.WriteLine();
        }

        Console.Read();
    }

Upvotes: 1

Related Questions