- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Swift Program to Get Odd and Even Numbers From the Array Using Library Function

This tutorial will discuss how to write swift program to get odd and even numbers from the array using library function.

Odd numbers are those number which are not completely divisible by 2, or in other words when a number divided by 2 and leave some remainder then such type of numbers are known as odd numbers. Following is the list of some odd numbers −

1, 3, 5, 7, 11, 13, …..

Even numbers are those numbers which are completely divisible by 2, or in other words when a number is divided by 2 and leaves 0 remainder the such type of numbers are known as even numbers. Following is the list of some even numbers −

2, 4, 6, 8, 10, 12, 14, ……..

An array is an ordered collection which is used to store same type of data. In an array, Swift provide an in-built function named as filter(). This function returns all the items from the array that satisfies the given condition. So we use filter() to filter out odd and even numbers from the given integer array.

Below is a demonstration of the same −

Suppose our given input is −

MyArray = [23, 5, 6, 7, 89, 64, 2, 19]

The desired output would be −

Odd Numbers = [6, 64, 2] Even Numbers = [23, 5, 7, 89, 19]

## Syntax

Following is the syntax −

Output = myArr.filter({condition})

Below is a demonstration of the same −

## Algorithm

Following is the algorithm −

**Step 1**− Create an array of integer type.**Step 2**− Finding the odd and even numbers from the array using filter() function and store the result into res variable −

For odd numbers −

var minElement = myarray.min()!

For even numbers −

var res = myNumber.filter({$0 % 2 == 0})

Note − Here $0 is the shorthand representation of first argument.

**Step 3**− Print the output

## Example 1

The following program shows how to get odd numbers from the array using library function.

import Foundation import Glibc // Creating an array of Integer Type var myNumber = [23, 5, 6, 7, 89, 64, 2, 19] print("Original array: ", myNumber) // Finding the odd numbers from the array var res = myNumber.filter({$0 % 2 != 0}) print("Odd Elements:", res)

## Output

Original array: [23, 5, 6, 7, 89, 64, 2, 19] Odd Elements: [23, 5, 7, 89, 19]

Here, in the above code, we have an array of integer type = [23, 5, 6, 7, 89, 64, 2, 19]. Now we find odd numbers from the array using the filter() function, In this function, we pass a condition that is {$0 % 2 != 0}, this condition will check the remainder is not equal to 0. If the remainder is not equal to 0(condition is true), then this function will return the number. Otherwise it will move to next number to check for odd number. So the working of the above code is −

var res = myNumber.filter({$0 % 2 != 0}) = myNumber.filter({23 % 2 != 0}) = myNumber.filter({1 != 0}) = condition is true so return 23 var res = myNumber.filter({5 % 2 != 0}) = myNumber.filter({1 != 0}) = condition is true so return 5 var res = myNumber.filter({6 % 2 != 0}) = myNumber.filter({0 != 0}) = condition is false return nothing

… so on till the last element.

So the output is [23, 5, 7, 89, 19].

## Example 2

The following program shows how to get even numbers from the array using library function.

import Foundation import Glibc // Creating an array of Integer Type var myNumber = [23, 5, 6, 7, 89, 64, 2, 19] print("Original array: ", myNumber) // Finding the even numbers from the array var res = myNumber.filter({$0 % 2 == 0}) print("Even Elements:", res)

## Output

Original array: [23, 5, 6, 7, 89, 64, 2, 19] Even Elements: [6, 64, 2]

Here, in the above code, we have an array of integer type = [23, 5, 6, 7, 89, 64, 2, 19]. Now we find even numbers from the array using the filter() function, In this function, we pass a condition that is {$0 % 2 == 0}, this condition will check the remainder is equal to 0. If the remainder is equal to 0(condition is true), then this function will return the number. Otherwise it will move to next number to check for odd number. So the working of the above code is;

var res = myNumber.filter({$0 % 2 != 0}) = myNumber.filter({23 % 2 != 0}) = myNumber.filter({1 != 0}) = condition is false so return nothing var res = myNumber.filter({5 % 2 != 0}) = myNumber.filter({1 != 0}) = condition is false so return nothing var res = myNumber.filter({6 % 2 != 0}) = myNumber.filter({0 != 0}) = condition is true return 6

… so on till the last element.

So the output is [6, 64, 2].

- Related Questions & Answers
- Java program to Print Odd and Even Number from an Array
- Even numbers at even index and odd numbers at odd index in C++
- Java program to print odd and even number from a Java array.
- Write a Golang program to find odd and even numbers using bit operation
- How to find the Odd and Even numbers in an Array in java?
- Python program to Count Even and Odd numbers in a List
- How to get odd and even position characters from a string?
- C program to store even, odd and prime numbers into separate files
- How to separate even and odd numbers in an array by using for loop in C language?
- Largest Even and Odd N-digit numbers in C++
- Count Odd and Even numbers in a range from L to R in C++
- Adding only odd or even numbers JavaScript
- Program to sort all even and odd numbers in increasing and decreasing order respectively in Python
- C++ program for the Array Index with same count of even or odd numbers on both sides?
- Missing even and odd elements from the given arrays in C++
- C# program to split the Even and Odd integers into different arrays