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:
- Calculate the sum of first n natural numbers as sumtotal= n*(n+1)/2
- Create a variable sum to store the sum of array elements.
- Print the missing number as sumtotal – sum