Created
February 8, 2022 21:37
-
-
Save colindooley11/aded1e1ee082636207dcf8a7f01c4002 to your computer and use it in GitHub Desktop.
#UniqueInOrder #codewars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Mine | |
using System.Collections.Generic; | |
using System.Linq; | |
public static class Kata | |
{ | |
public static IEnumerable<T> UniqueInOrder<T>(IEnumerable<T> iterable) | |
{ | |
var output = new List<T>(); | |
T newItem = default(T); | |
foreach(var item in iterable) | |
{ | |
if (!item.Equals(newItem)) | |
{ | |
newItem = item; | |
output.Add(item); | |
} | |
} | |
return output.Select(x => x); | |
} | |
} | |
// better (dont forget to yield we are returning an enumerable!!) | |
public static class Kata | |
{ | |
public static IEnumerable<T> UniqueInOrder<T>(IEnumerable<T> iterable) | |
{ | |
T previous = default(T); | |
foreach(T current in iterable) | |
{ | |
if (!current.Equals(previous)) | |
{ | |
previous = current; | |
yield return current; | |
} | |
} | |
} | |
} | |
//Clever | |
public static class Kata | |
{ | |
public static IEnumerable<T> UniqueInOrder<T>(IEnumerable<T> iterable) | |
{ | |
return iterable.Where((x, i) => i == 0 || !Equals(x, iterable.ElementAt(i-1))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment