Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string.The input string can be assumed to contain only alphanumeric characters, including digits, uppercase and lowercase alphabets.
Example
1 2 3 4 5 6
"abcde" -> 0 # no characters repeats more than once "aabbcde" -> 2 # 'a' and 'b' "aabbcdeB" -> 2 # 'a' and 'b' "indivisibility" -> 1 # 'i' "Indivisibilities" -> 2 # 'i' and 's' "aa11" -> 2 # 'a' and '1'
public class Kata { public static int DuplicateCount(string str) { // 将字符串转成char数组 char[] charArr = str.ToLower().ToCharArray(); // 定义int类型数组,用于储存字符重复次数 int[] arr = new int[123]; foreach (char item in charArr) { // 将char类型转成int类型,并作为数组的key arr[(int)item]++; } // 找出重复次数大于2的字符,并返回总数 return arr.Where(e => e >= 2).Count(); } }
其他网友的答案
1 2 3 4 5 6 7 8
using System; using System.Linq; public class Kata{ public static int DuplicateCount(string str) { return str.ToList().GroupBy(x => Char.ToLower(x)).ToDictionary(x => x.Key, x => x.Count()).Where(d => d.Value >= 2).Select(v => v).Count(); } }
1 2 3 4 5 6 7 8 9 10
using System; using System.Text.RegularExpressions; using System.Linq; public class Kata{ public static int DuplicateCount(string str) { str = String.Join("", str.ToLower().OrderBy(c => c)); return Regex.Matches(str, @"(.)\1+").Count; } }