Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace FilterTask
- {
- public static class ArrayExtension
- {
- /// <summary>
- /// Returns new array of elements that contain expected digit from source array.
- /// </summary>
- /// <param name="source">Source array.</param>
- /// <param name="digit">Expected digit.</param>
- /// <returns>Array of elements that contain expected digit.</returns>
- /// <exception cref="ArgumentNullException">Thrown when array is null.</exception>
- /// <exception cref="ArgumentException">Thrown when array is empty.</exception>
- /// <exception cref="ArgumentOutOfRangeException">Thrown when digit value is out of range (0..9).</exception>
- /// <example>
- /// {1, 2, 3, 4, 5, 6, 7, 68, 69, 70, 15, 17} => { 7, 70, 17 } for digit = 7.
- /// </example>
- public static int[] FilterByDigit(int[]? source, int digit)
- {
- int length = source.Length;
- int[] result = new int[length];
- int k = 0;
- for (int i = 0; i < length; i++)
- {
- int dump = Math.Abs(source[i]);
- do
- {
- if (dump % 10 == digit)
- {
- result[k] = source[i];
- k++;
- break;
- }
- dump /= 10;
- }
- while (dump > 0);
- }
- Array.Resize(ref result, k);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement