Skip to content

Instantly share code, notes, and snippets.

@fredyfx
Created May 3, 2018 20:49
Show Gist options
  • Save fredyfx/0309bcb057662aaa7fc9cb10b18bf1a4 to your computer and use it in GitHub Desktop.
Save fredyfx/0309bcb057662aaa7fc9cb10b18bf1a4 to your computer and use it in GitHub Desktop.
Jugando con los Generics
// Note that you can return any data structure you want, as long it inherits the IEnumerable interface.
UniqueInOrder("AAAABBBCCDAABBB") => "ABCDAB"
UniqueInOrder("ABBCcAD") => "ABCcAD"
UniqueInOrder("12233") => "123"
UniqueInOrder(new List<double> {1.1, 2.2, 2.2, 3.3}) => new List<double> {1.1, 2.2, 3.3}
using System.Collections.Generic;
public static class Kata
{
public static IEnumerable<T> UniqueInOrder<T>(IEnumerable<T> iterable)
{
List<T> result = new List<T>();
T pre_item = default(T);
foreach (T item in iterable) {
if(!item.Equals(pre_item)){
result.Add(item);
pre_item = item;
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment