C # How can I get numbers containing number x from a list?

    static void Main(string[] args)
    {
        List<int> Allnumber = new List<int>();
        Random rnd = new Random();
        while (true)
        {
            int dice = rnd.Next(1, 100);
            Console.WriteLine("Random number between 1 and 100 : {0}", dice);
            Allnumber.Add(dice);
            if (dice == 1)
                break;
        }

        Console.WriteLine();
        Console.WriteLine("Allnumber : " + string.Join(" ", Allnumber));
        List<int> Odd = (from number in Allnumber where number % 2 != 0 select number).ToList();
        List<int> Even = new List<int>(from number in Allnumber where number % 2 == 0 select number);
        Console.WriteLine("Odd : " + string.Join(" ", Odd));
        Console.WriteLine("Even : " + string.Join(" ", Even));

I want to create a new list that includes 3 from the Allnumber list. It must contain the whole number that has 3 (3, 13, 23, 33, 34, 36, 39, 43, 53 ...). In any case, to pick up only 3? I found that there are Findall, Contain methods, but I can not use it for a list of int types. THANKS YOU EVERYONE, do not believe that there are so many ways to do this: D

+4
source share
5 answers

I would move this check to a separate method

public static bool ContainsDigit(int input, int digit)
{
    do
    {
        if (input % 10 == digit)
        {
            return true;
        }
        input /= 10;
    }
    while (input > 0);
    return false;
}

using:

List<int> result = Allnumber.Where(x => ContainsDigit(x, 3)).ToList();

https://dotnetfiddle.net/2ZPNbM


Same approach on one line

List<int> Allnumber = Enumerable.Range(0, 100).ToList();
List<int> result = Allnumber.Where(x => { do { if (x % 10 == 3) return true; } while ((x /= 10) > 0); return false; }).ToList();

Run (Allnumber with numbers 1000000):

|------------------------------------------------------|
| User       | Method                   | Time         |
|------------|--------------------------+--------------|
| fubo       | ContainsDigit()          | 0,03 seconds |
| JamieC     | ToString().Contains("3") | 0,20 seconds |
| TheGeneral | WhereDigit()             | 0,10 seconds |
| TheGeneral | InternalRun()            | 0,04 seconds |
|------------------------------------------------------|

dotnetfiddle benachmarking - , , - dotnetfiddle, 100,000 1,000,000 , ... https://dotnetfiddle.net/pqCx2J

+8

, yield IEnumerable

public static class StupidExtensions
{
    public static IEnumerable<int> Digits(int input)
    {
        do yield return input % 10; while ((input /= 10) > 0);
    }

    public static IEnumerable<int> WhereDigit(this IEnumerable<int> source, int digit) 
              => source.Where(x => Digits(x).Contains(digit));
}

var result = Allnumber.WhereDigit(3);


2

protected override IEnumerable<int> InternalRun(IEnumerable<int> values, int digit)
{
   var ary = values.ToArray();
   var result = new List<int>();
   fixed (int* pAry = ary)
   {
      for (var p = pAry; p < pAry + ary.Length; p++)
         for (var d = *p; d > 0; d /= 10)
            if (d % 10 == digit){ result.Add(*p); break;}
   }  
}

protected override IEnumerable<int> InternalRun(IEnumerable<int> values, int digit)
{
   foreach (var val in values)
      for (var d = val; d > 0; d /= 10)
         if (d % 10 == digit)
            yield return val;
}
+3

, Findall, Contain, int.

, , where

 List<int> HasThrees = (from number in Allnumber where number.ToString().Contains("3") select number).ToList();

. , .

, , .

+2

First convert all the numbers in the sentence Whereto strings with ToString, and then use Containsto get a list of all numbers containing the number 3:

var result = Allnumber.Where(x => x.ToString().Contains("3")).ToList();
+2
source

You just need to check if remainder 3 remains after dividing it by 10, i.e. n% 10 == 3.

IEnumerable<int> allThrees =
    from num in Allnumber
    where num%10 == 3
    select num;

This solution works based on the examples included in the OP, after reading the comment, I understand that the question is about all 3s, such as 31.32. Then string manipulation may be the solution.

-1
source

Source: https://habr.com/ru/post/1696142/


All Articles