using System;using System.Linq;publicclassKata{publicstaticboolOne(int[] arr, Func<int,bool> fun){return arr.Count(fun)==1;}}
答案2:
using System;using System.Linq;publicclassKata{publicstaticboolOne(int[] arr, Func<int,bool> fun){return arr.Where(fun).Count()==1;}}
答案3:
using System;using System.Linq;publicclassKata{publicstaticboolOne(int[] arr, Func<int,bool> fun){// One-liner// return arr.Count(i => fun(i)) == 1;// Stops calling func and returns early if we know we've got more than oneint count =0;for(int i =0; i < arr.Length && count <2;++i){if(fun(arr[i])){++count;}}return count ==1;}}
答案4:
using System;using System.Linq;publicclassKata{publicstaticboolOne(int[] arr, Func<int,bool> fun)=> arr.Count(x =>fun(x))==1;}
答案5:
using System;using System.Linq;publicclassKata{publicstaticboolOne(int[] arr, Func<int,bool> fun){return arr.Select(x =>fun(x)).Where(x => x ==true).Count()==1;}}
using System;publicclassKata{publicstaticboolOne(int[] arr, Func<int,bool> fun){int trues =0;foreach(int i in arr){if(fun(i)){
trues++;}}return trues ==1;}}
答案8:
using System;using System.Linq;publicclassKata{publicstaticboolOne(int[] arr, Func<int,bool> fun){return(arr.Where(x =>fun(x)==true)).Count()==1;}}
答案9:
using System;using System.Linq;publicclassKata{publicstaticboolOne(int[] arr, Func<int,bool> fun){return arr.Select(fun).ToArray().Count(i => i ==true)==1;}}
答案10:
using System;publicclassKata{publicstaticboolOne(int[] arr, Func<int,bool> fun){bool found =false;for(int i =0; i < arr.Length; i++){if(fun(arr[i])){if(found)returnfalse;
found =true;}}return found;}}