user11618004
user11618004

Reputation:

how can I count a number from a string

string val = "4 12 441 662 2234 441 441";       

I'm trying to count duplicates after the counting the duplicates are getting removed. Currently I'm stuck and can't go forward.

I want this output 4 12 662 2234 3x 441

Upvotes: 0

Views: 110

Answers (6)

Krishna Varma
Krishna Varma

Reputation: 4250

Using LINQ

string val = "4 12 441 662 2234 441 441";
var result = val.Split(" ")
                .GroupBy(x => x)
                .ToDictionary(y => y.Key, y => y.Count())
                .OrderBy(z => z.Value)
                .Select(x=> x.Value == 1 ? $"{x.Key}" : $"{x.Value}x {x.Key}")
                .ToList();

Console.WriteLine(string.Join(" ", result));

OUTPUT

4 12 662 2234 3x 441

Upvotes: 1

Matthew Watson
Matthew Watson

Reputation: 109537

You can do this with Linq like so:

string val = "4 12 441 662 2234 441 441";

var groupedValues = 
    val.Split(' ')
   .GroupBy(x => x)
   .OrderBy(g => g.Count())
   .Select(g => g.Count() > 1 ? g.Count() + "x " + g.Key : g.Key);

var result = string.Join(' ', groupedValues);

Console.WriteLine(result); // 4 12 662 2234 3x 441 

It would be slightly more efficient to write the Linq as follows to avoid recalculating Count() twice:

var groupedValues = 
    val.Split(' ')
   .GroupBy(x => x)
   .Select(f => (count: f.Count(), value: f.Key))
   .OrderBy(g => g.count)
   .Select(h => h.count > 1 ? h.count + "x " + h.value : h.value);

Note the .OrderBy() which ensures that the repeated values go at the end of the output, as per your original requirement.

Upvotes: 2

J.F.
J.F.

Reputation: 15177

I think this could work:

First of all get your string as List

string val = "4 12 441 662 2234 441 441";
var list = val.Split(' ').ToList();

Then get the values repeated and store into a object with Value and Count:

var query = list.GroupBy(r => r)
            .Select(grp => new
            {
              Value = grp.Key,
              Count = grp.Count()
            });

Now you have something like [{Value: X, Count: X}, {...}, {...} ... ]

And after that iterate over this list you can create a new string to output

var str = "";
foreach (var item in query)
{
  if(item.Count > 1)
  {
    str += item.Count+"x ";
  }
  str+=item.Value+" ";
}
Console.WriteLine("str = "+str);

The output is: str = 4 12 3x 441 662 2234

Upvotes: 0

Simon Danninger
Simon Danninger

Reputation: 458

You can use linq.

  1. split String into parts.
  2. Group all the same with the GroupBy Expression. (Grouping has aKey and a List containing all the Matches)
  3. Aggregate this takes a starting String and a expression how to handle ongoing elements
string val = "4 12 441 662 2234 441 441";
                
var splitted = val.Split(' ');

var resultString = splitted.ToList()
   .GroupBy(el => el)
   .Select(grouping => grouping.Count() + "x " + grouping.Key)
   .Aggregate("", (s1, s2) => s1 + s2 + ", ");

   Console.WriteLine(resultString);

Upvotes: 1

Rand Random
Rand Random

Reputation: 7440

Why Regular Expression at all?

string val = "4 12 441 662 2234 441 441";
Console.WriteLine(string.Join(" ", val.Split(' ').GroupBy(x => x).Select(x => x.Count() > 1 ? $"{x.Count()}x {x.Key}" : x.Key)));

Upvotes: 2

komal shehzadi
komal shehzadi

Reputation: 46

You can create an array of digits in a string by using a space delimiter to separate them after that it is much easier to find out the duplicates in an array.

Upvotes: 1

Related Questions