Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,708 questions

55,472 answers

573 users

How to split an array of int numbers into two groups by condition with C#

1 Answer

0 votes
using System;
using System.Linq;

class Program
{
    static void Main() {
        int[] arr = { 2, 5, 4, 6, 8, 7, 9, 1, 3 };

        var all_groups = from n in arr
                        group n by (n % 2 == 0) into groups
                        select groups;
    
        foreach (IGrouping<bool, int> group in all_groups) {
            if (group.Key == true)
                Console.WriteLine("Divisible by 2");
            else
                Console.WriteLine("Not Divisible by 2");
    
            foreach (int number in group)
                Console.WriteLine(number);
        }
    }
}



/*
run:

Divisible by 2
2
4
6
8
Not Divisible by 2
5
7
9
1
3

*/

 



answered Mar 10, 2021 by avibootz
edited Mar 10, 2021 by avibootz
...