Thursday, November 26, 2020

 Find the Index of element in Array in swift.

        swift 5 , Swift 4.2 

        As a swift find the element in array without pre-define function.

   
     As a swift find the element in array with pre-define function.

 

Program for array rotation In Swift.

 

METHOD 1 (Using temp array) 



METHOD 2 (Rotate one by one)
















METHOD 3 (A Juggling Algorithm) 




Find the Missing Number

Method 1: This method uses the technique of the summation formula. 

  • Approach: The length of the array is n-1. So the sum of all n elements, i.e sum of numbers from 1 to n can be calculated using the formula n*(n+1)/2. Now find the sum of all the elements in the array and subtract it from the sum of first n natural numbers, it will be the value of the missing element.
  • Algorithm: 
    1. Calculate the sum of first n natural numbers as sumtotal= n*(n+1)/2
    2. Create a variable sum to store the sum of array elements.
    3. Print the missing number as sumtotal – sum




No comments:

Ios Interview Questions

  Automatic reference counting | What is swift ARC | Memory management in swift Code:- class Person { var name: String weak var ...