Taha Rehman Siddiqui
Taha Rehman Siddiqui

Reputation: 2533

Find string with most frequency of a character in List of strings using LINQ C#

I am dealing with binary vectors so each string in a List<string> looks like

vectors[0] = "1 0 0 0 1 1 1 0 0 0 1 0 1 1 0 0 0 0 1 0 0 1"; 
vectors[1] = "1 0 0 0 0 1 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1";

I want to get the biggest string from the List<string>, with most number of 1's.

Upvotes: 1

Views: 516

Answers (8)

ocuenca
ocuenca

Reputation: 39376

I would do this:

var biggest= vectors.Select(v=> new {Vector = v, Count = v.Count(c => c=='1')})
                    .Aggregate((seed, current) => seed.Count < current.Count ? current:seed)
                    .Vector;

You can also use OrderBy extension method but Aggregate is O(n) while OrderBy is O(n* log n).

I first call Select extension to avoid calculate multiple times the amount of 1's of seed:

var biggest= vectors.Aggregate((seed, current)=>seed.Count(c=>c=='1')<current.Count(c=>c=='1')?current:seed);

Upvotes: 3

CSharpie
CSharpie

Reputation: 9477

I feel like the provided solutions here are way too complicated. So here is mine:

vectors.OrderByDescending(v => v.Count(c => c == '1')).First();

Note that Count is only evaluated once per "Vector". EnumerableSorter does this for you.

If you want a more performant solution then go with @octavioccls answer

Upvotes: 3

AD.Net
AD.Net

Reputation: 13409

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        var vectors = new List<string>();
        vectors.Add("1 0 0 0 1 1 1 0 0 0 1 0 1 1 0 0 0 0 1 0 0 1"); 
        vectors.Add("1 0 0 0 0 1 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1");


        foreach(var v in vectors)
        {
            var result = new List<string>();
            int max = 0;
            foreach(var c in v)
            {
                if(string.IsNullOrEmpty(c.ToString().Trim())) continue;
                //Console.Write(c);
                //Console.Write(max);
                if(c != '1')
                {
                    max = 0;
                }
                else
                {
                    max++;
                    if(max > result.Count)
                    {
                        result.Add(c.ToString());
                    }
                }
            }

            Console.WriteLine("Final Result: " + string.Join(" ", result));
        }
    }
}

Upvotes: 0

Ali Humayun
Ali Humayun

Reputation: 1824

vectors.OrderByDescending(b => b.Split('1').Length).First()

Upvotes: 1

Shyju
Shyju

Reputation: 218872

Using LINQ

var result = vectors.Select(d => new { Item = d,
                                       OneCount = d.Split(' ').Count(y => y == "1")})
                    .OrderByDescending(t => t.OneCount)
                    .Select(k=>k.Item).FirstOrDefault();

Upvotes: 1

Matthew Whited
Matthew Whited

Reputation: 22443

var vectors = new[] { 
    "1 0 0 0 1 1 1 0 0 0 1 0 1 1 0 0 0 0 1 0 0 1",
    "1 0 0 0 0 1 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1",
};
var q = from v in vectors
        select new
        {
            v,
            c = v.Split(' ').Count(b => b == "1"),
        };
var m = q.First(c => c.c == q.Max(b => b.c)).v;

Upvotes: 0

granmirupa
granmirupa

Reputation: 2790

You can do a loop on the list and for each string calculate the number of occurences of '1' in this way:

int count = myString.Split('1').Length - 1;

Then store a temporary variable with the local maximum and the number of string with that maximum. Like this:

int max = list[0].Split('1').Length - 1;
int stringMax = 0;

for (int i = 1; i < list.Length; i++)
{
    var count = list[i].Split('1').Length - 1;
    if (count > max)
    {
        max = count;
        stringMax = i;
    }
}

At the end stringMax is the string with max value and max is that value

Upvotes: 0

Roman
Roman

Reputation: 12201

You can use Count() method:

int index = 0;
int max = vectors[0].Count(x => x == '1');

for (int a = 1; a < vectors.Length; a++)
{
    var count = vectors[a].Count(x => x == '$');
    if (count > max)
    {
        max = count;
        index = a;
    }
}

after loop you will have index of string which has maximum '1'

Upvotes: 1

Related Questions