获取数组中出现的字符数是偶数还是奇数? [英] Get number of occurrences of character in an array is even or odd ?

查看:252
本文介绍了获取数组中出现的字符数是偶数还是奇数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在接受采访时被问到一个问题,以获得字符串数组中出现的字符数是偶数还是奇数?例如,我们有一个这样的数组。



D,E,X,W,R,X,W,X,Q,O,X



现在怎样才能找到X在偶数次或奇数次发生而不使用if条件?

解决方案

你可以使用Linq Grouping来完成这项工作

  char  [] letters = {'  D''  E''  X''  W''  R''  X''  W''  X''  Q''  O''  X'}; 
var characterGroups =
letters.GroupBy(c = > c)。 GroupBy(g = > g.Count()% 2 == 1 )ToList();
var OddGroup = characterGroups.Where(c = > c.Key);
var EvenGroup = characterGroups.Where(c = > !c.Key);
Console.WriteLine( 出现奇数次的字符是:) ;
foreach var charGroup in OddGroup.SelectMany(grouping = > 分组))
{
Console.WriteLine(charGroup.Key);
}
Console.WriteLine( 偶数次出现的字符是: );
foreach var charGroup in EvenGroup.SelectMany(grouping = > 分组))
{
Console.WriteLine(charGroup.Key);
}


我建​​议用一行来判断字符串中出现'X'的次数是奇数还是偶数。它使用Linq和条件赋值。

  string  s =   DEXWRXWXQOX; 
string result =((s.Where(c = > c == ' X')。Count()& 1 )== 0 )? even odd;


如果要返回每个字符的记录集和每个字符的出现次数和信息关于偶数/奇数,试试这个:

  char  [] letters = {'  D''  E''  X' '  W''  R''  X''  W''  X''  Q''  O''  X'}; 

var result = letters.GroupBy(a => a)
。选择(g => new
{
Character = g.Key,
Count = g.Count(),
IsEvenOrOdd = g.Count()% 2 == 0 甚至 奇数
});





结果:

 字符数IsEvenOrOdd  
D 1 奇数
E 1 奇数
X 4 甚至
W 2 甚至
R 1 奇数
Q 1 奇数
O 1 O DD


Hi,

I happened to be asked an question in an interview to get number of occurrences of character in string array is even or odd ? For example we have an array like this.

D,E,X,W,R,X,W,X,Q,O,X

Now how can be find that X is occurring in even times or odd times without using if condition ?

解决方案

You can use Linq Grouping to do the job

char[] letters = { 'D', 'E', 'X', 'W', 'R', 'X', 'W', 'X', 'Q', 'O', 'X' };
            var characterGroups =
                letters.GroupBy(c => c).GroupBy(g => g.Count() % 2 == 1).ToList();
            var OddGroup = characterGroups.Where(c => c.Key);
            var EvenGroup = characterGroups.Where(c => !c.Key);
            Console.WriteLine("The characters that occur an odd number of times are :");
            foreach (var charGroup in OddGroup.SelectMany(grouping => grouping))
            {
                Console.WriteLine(charGroup.Key);
            }
            Console.WriteLine("The characters that occur an even number of times are :");
            foreach (var charGroup in EvenGroup.SelectMany(grouping => grouping))
            {
                Console.WriteLine(charGroup.Key);
            }


I propose one line to get whether the number of occurrences of 'X' in the string is odd or even. It uses Linq and a conditional assignment.

string s = "DEXWRXWXQOX";
string result = ((s.Where(c => c == 'X').Count() & 1) == 0) ? "even" : "odd";


If you want to return recordset of characters with number of occurences of each of them and information about even/odd, try this:

char[] letters = { 'D', 'E', 'X', 'W', 'R', 'X', 'W', 'X', 'Q', 'O', 'X' };

var result = letters.GroupBy(a=>a)
    .Select(g=>new
        {
            Character = g.Key,
            Count = g.Count(),
            IsEvenOrOdd = g.Count() % 2==0 ? "Even" : "Odd"
        });



Result:

Character Count IsEvenOrOdd
D         1         Odd
E         1         Odd
X         4         Even
W         2         Even
R         1         Odd
Q         1         Odd
O         1         Odd


这篇关于获取数组中出现的字符数是偶数还是奇数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆