Swift Program to Remove All Occurrences of an Element in an Array


In this article, we will learn how to write a swift program to remove all occurrences of an element in an array. So here we use the following methods 

  • Using an inbuilt function

  • Without using an inbuilt function

Method 1: Using the removeAll() Function

To remove all the occurrences of an element in an array we use a pre-defined function named removeAll(where:). This function removes all the elements from the given sequence that satisfy the given condition. For Example 

Array = [2, 3, 5, 67, 2, 87, 2, 68]
Element = 2
Output array = [3, 5, 67, 87, 68]

So here we delete all the occurrences of 2 in the original array and display the remaining elements in an array in the output.

Syntax

Following is the syntax −

mutating func removeAll(where removeElement:(self.Element) throws ->Bool)rethrows

Here removeElement is a closure that takes an item of the given sequence as its argument and returns a boolean value representing whether the given item is removed from the given sequence.

Algorithm

  • Step 1 − Create an array.

  • Step 2 − Removing all the occurrences of the element using the removeAll(where:) method.

Array.removeAll{$0 == 6}
  • Step 3 − Print the output

Example

import Foundation
import Glibc
 
// Creating an array of integer type
var Array : [Int] = [34, 6, 7, 81, 6, 56, 6, 32, 6, 6]

print("Original array:", Array)

// Deleting all the occurrences of 6 in the given array
// Using removeAll(where:) method
Array.removeAll{$0 == 6}

print("Modified array", Array)

Output

In the following example, we use the removeAll(where:) method to remove all the occurrences of the given number from the array.

Original array: [34, 6, 7, 81, 6, 56, 6, 32, 6, 6]
Modified array [34, 7, 81, 56, 32]

Here in the above code, we create an array of integer types with values: [34, 6, 7, 81, 6, 56, 6, 32, 6, 6]. Where 6 appears 5 times in the array now we want to remove all the 5 appearances of the 6 from the array. So we use removeAll{$0 == 6}, here $0 represents the first element of the array passed in the closer. The removeAll{$0 == 6} method check each element present in the array from index 0 to end and remove only those element that satisfies the given condition.

Method 2: Without using an inbuilt Function

To remove all the occurrences of an element in an array we use conditional statements. Here conditional statements check each element with the element which you want to delete from the array. If the condition is true, then it skips that element and prints the remaining array of elements.

Algorithm

  • Step 1 − Create a function.

  • Step 2 − Find the size of the array using the count property.

  • Step 3 − Use for loop to iterate through each element of the array. Iteration started from 0 to array size −1

  • Step 4 − Check if the given array element is equal to the number using an if−else statement. If the current array element is equal to the given number, then continue the loop otherwise print the array.

  • Step 5 − Outside the function creates an array.

  • Step 6 − Assign the number which you want to delete in a variable.

  • Step 7 − Cal the function and pass the array and number(which you want to delete) as a parameter.

  • Step 8 − Print the output.

Example

In the following example, we remove all the occurrences of the given number from the array using a conditional statement.

import Foundation
import Glibc

// Creating a function to delete all the 
// occurrence of 1 from the given array 
func DeleteElement(arr:[Int], num: Int) {
   let arrSize = arr.count
   
   print("Modified Array:")
   
   // Iterate through each element of the array
   for x in 0..<arrSize {
   
      // If the array element is equal to the given num
      // then continue the loop otherwise print the array
      if (arr[x] == num) {
         continue
      } else {
         print(arr[x], terminator:",")
         
      }
   }
}
// Creating an array of integer type
var Array : [Int] = [24, 1, 7, 91, 1, 56, 1, 42, 1, 0]
print("Original array:", Array)
let element = 1

// Calling the function 
DeleteElement(arr:Array, num: element)

Output

Original array: [24, 1, 7, 91, 1, 56, 1, 42, 1, 0]
Modified Array:
24,7,91,56,42,0,

Here in the above code, we create a function named DeleteElement() which takes two arguments an array and the num(whose all the occurrences we want to remove). In this function, we iterate through each array element and compare with the num using the == operator. If the condition is true, then it continues the loop. Otherwise, it will print the remaining array.

Conclusion

Hence to remove all the occurrences of an element from the array we can use either an inbuilt function or condition statement according to your requirement. Both methods complete the given task very well.

Updated on: 20-Dec-2022

593 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements