Solutions
LeetCode
LeetCode
  • Introduction
  • Problems
    • Algorithms
      • Easy
        • 844. Backspace String Compare
        • 905. Sort Array By Parity
    • Database
      • Easy
        • #175 Combine Two Tables
        • #181 Employees Earning More Than Their Managers
        • #182 Duplicate Emails
        • #183 Customers Who Never Order
        • #196 Delete Duplicate Emails
        • #197 Rising Temperature
        • #511 Game Play Analysis I
        • #584 Find Customer Referee
        • #586 Customer Placing the Largest Number of Orders
        • #595 Big Countries
        • #596 Classes More Than 5 Students
        • #607 Sales Person
        • #620 Not Boring Movies
        • #627 Swap Salary
        • #1050 Actors and Directors Who Cooperated At Least Three Times
        • #1084 Sales Analysis III
        • #1141 User Activity for the Past 30 Days I
        • #1148 Article Views I
        • #1179 Reformat Department Table
        • #1407 Top Travellers
        • #1484 Group Sold Products By The Date
        • #1527 Patients With a Condition
        • #1581 Customer Who Visited but Did Not Make Any Transactions
        • #1587 Bank Account Summary II
        • #1667 Fix Names in a Table
        • #1693 Daily Leads and Partners
        • #1729 Find Followers Count
        • #1741 Find Total Time Spent by Each Employee
        • #1757 Recyclable and Low Fat Products
        • #1795 Rearrange Products Table
        • #1873 Calculate Special Bonus
        • #1890 The Latest Login in 2020
        • #1965 Employees With Missing Information
      • Medium
        • #176 Second Highest Salary
        • #177 Nth Highest Salary
    • Shell
      • Easy
        • #193 Valid Phone Numbers
        • #195 Tenth Line
Powered by GitBook
On this page

Was this helpful?

  1. Problems
  2. Algorithms
  3. Easy

905. Sort Array By Parity

https://leetcode.com/problems/sort-array-by-parity/

public class Solution {
    public int[] SortArrayByParity(int[] nums) {
        var countEven = 0;
        for(int i = 0; i < nums.Length; i++){
            if(nums[i] % 2 == 0){
                (nums[i], nums[countEven]) = (nums[countEven], nums[i]);
                countEven++;
            }
        }
        return nums;
    }
}
  • LINQ

public class Solution {
    public int[] SortArrayByParity(int[] nums) {
        var evenNumbers = nums.Where(n => n % 2 == 0);
        var oddNumbers = nums.Where(n => n % 2 == 1);
        return evenNumbers.Concat(oddNumbers).ToArray();
    }
}
Previous844. Backspace String CompareNextDatabase

Last updated 3 years ago

Was this helpful?