using System; using System.Linq; public class Kata { public static int DuplicateCount(string str) { return str .ToLower() .GroupBy(c => c) .Aggregate(0, (current, next) => current + (next.Count() > 1 ? 1 : 0)); } }
- using System;
- using System.Linq;
- public class Kata
- {
- public static int DuplicateCount(string str)
- {
var orderedLowercase = str.ToLower().OrderBy(c => c);var countDuplicates = 0;var countOccurrenciesCurrentCharacter = 1;char? previousElement = null;var firstElement = true;foreach(var currentElement in orderedLowercase){if (firstElement){firstElement = false;}else{if (currentElement == previousElement.Value){countOccurrenciesCurrentCharacter ++;if (countOccurrenciesCurrentCharacter == 2){countDuplicates ++;}}else{countOccurrenciesCurrentCharacter = 1;}}previousElement = currentElement;}return countDuplicates;- return str
- .ToLower()
- .GroupBy(c => c)
- .Aggregate(0, (current, next) => current + (next.Count() > 1 ? 1 : 0));
- }
- }
That's my first kumite... don't really know what all this is about...
using System;
using System.Linq;
public class Kata
{
public static int DuplicateCount(string str)
{
var orderedLowercase = str.ToLower().OrderBy(c => c);
var countDuplicates = 0;
var countOccurrenciesCurrentCharacter = 1;
char? previousElement = null;
var firstElement = true;
foreach(var currentElement in orderedLowercase)
{
if (firstElement)
{
firstElement = false;
}
else
{
if (currentElement == previousElement.Value)
{
countOccurrenciesCurrentCharacter ++;
if (countOccurrenciesCurrentCharacter == 2)
{
countDuplicates ++;
}
}
else
{
countOccurrenciesCurrentCharacter = 1;
}
}
previousElement = currentElement;
}
return countDuplicates;
}
}
namespace Solution {
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
[TestFixture]
public class KataTest
{
[Test]
public void KataTests()
{
Assert.AreEqual(0, Kata.DuplicateCount(""));
Assert.AreEqual(0, Kata.DuplicateCount("abcde"));
Assert.AreEqual(2, Kata.DuplicateCount("aabbcde"));
Assert.AreEqual(0, Kata.DuplicateCount("a"));
Assert.AreEqual(1, Kata.DuplicateCount("aa"));
Assert.AreEqual(2, Kata.DuplicateCount("aabBcde"), "should ignore case");
Assert.AreEqual(1, Kata.DuplicateCount("Indivisibility"));
Assert.AreEqual(2, Kata.DuplicateCount("Indivisibilities"), "characters may not be adjacent");
}
}
}