Skip to content

Commit

Permalink
UPDATED - Added new methods to DmjrsLinqExtensions class
Browse files Browse the repository at this point in the history
- Added "GreatestPowerTwo" method that returns the greatest power of IEnumerable of ints overloadedTwo" method a custom type and returns the greatest power of two based on the selected int value
  • Loading branch information
dgmjr committed Nov 23, 2023
1 parent 26e9af0 commit b401c71
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/System.Linq/System.Linq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,26 @@ public static IEnumerable<T> Prepend<T>(IEnumerable<T> values, T newValue)
yield return value;
}
}

public static int GreatestPowerOfTwo(this IEnumerable<int> values)
{
var n = values.Max();
var k = 1;
while (k < n)
{
k = k << 1;
}
return k;
}

public static int GreatestPowerOfTwo<T>(this IEnumerable<T> values, Func<T, int> selector)
{
var n = values.Max(selector);
var k = 1;
while (k < n)
{
k = k << 1;
}
return k;
}
}

0 comments on commit b401c71

Please sign in to comment.