1.Find sum of array:
def sum(arr):
sum=0
for i in arr:
sum=sum+i
return sum
arr=[]
arr=[5,2,19,97]
n=len(arr)
add=sum(arr)
print('Sum of the array is',add)
Output:
Sum of the array is 123
2.Find largest element in an array:
def largest(arr,n):
max=arr[0]
for i in range(1,n):
if(arr[i]>max):
max=arr[i]
return max
arr=[12,23,43,67]
n=len(arr)
lar=largest(arr,n)
print('Larest in the given array is',lar)
arr=[12,23,43,67]
max=arr[0]
n=len(arr)
for i in range(1,n):
if arr[i]>max:
max=arr[i]
print('largest in the given array is',lar)
Output:
largest in the given array is 67
3.Frequency of element in the array:
arr=[5,5,5,2,2,2,2,19,19,19,19,97,97,97,97,97,97]
val_count={}
for val in arr:
if val in val_count:
val_count[val]+=1
else:
val_count[val]=1
for key,value in val_count.items():
print(f"{key}: {value}")
Output:
5: 3
2: 4
19: 4
97: 6
4.Duplicate elements of an array:
arr=[5,5,2,2,19,97]
n=len(arr)
print("Duplicate elements in given array: ")
for i in range(0,n):
for j in range(i+1,n):
if arr[i]==arr[j]:
print(arr[j])
Output:
Duplicate elements in given array:
5
2
5.Left rotate the elements of an array:
arr = [11, 12, 13,14, 15]
n = 3
p=len(arr)
print("Original array: ")
for i in range(0, p):
print(arr[i])
for i in range(0, n):
first = arr[0]
for j in range(0, p-1):
arr[j] = arr[j+1]
arr[p-1] = first
print()
print("Array after left rotation: ")
for i in range(0, p):
print(arr[i])
Output:
Original array:
11
12
13
14
15
Array after left rotation:
14
15
11
12
13
6.Print the elements of an array:
arr=[5,2,19,97]
n=len(arr)
print("Elements of given array:")
for i in range(0,n):
print(arr[i],end=" ")
Output:
Elements of given array:
5 2 19 97
7.Elements of an array in reverse order:
1.Using List Slicing to Reverse an Array:
arr=[5,2,1,9,9,7]
print("Original array")
print(arr)
print("Array in reverse order")
res=arr[::-1]
print(res)
Output:
Original array
[5, 2, 1, 9, 9, 7]
Array in reverse order
[7, 9, 9, 1, 2, 5]
2.reverse() metod:
arr=[5,2,1,9,9,7]
print("Oriinal array")
print(arr)
print("Array in reverse order")
arr.reverse()
print(arr)
Output:
Oriinal array
[5, 2, 1, 9, 9, 7]
Array in reverse order
[7, 9, 9, 1, 2, 5]
8.Elements of an array present even position:
arr=[10,22,32,46,5]
n=len(arr)
for i in range(1,n,2):
print(arr[i])
Output:
22
46
9.Elements of an array present an odd position:
arr=[10,22,32,46,5]
n=len(arr)
for i in range(0,n,2):
print(arr[i])
Output:
10
32
5
10.Print the smallest element in an array:
arr=[5,2,19,97]
n=len(arr)
min=arr[0]
for i in range(1,n):
if arr[i]<min:
min=arr[i]
print("Smallest element present in given array: ")
print(min)
Output:
Smallest element present in given array:
2
11.Print the number of elements present in an array:
arr=[5,2,1,9,9,7,]
res=len(arr)
print("Number of elements present in given array: ")
print(res)
Output:
Number of elements present in given array:
6
12.Print the sum of all elements in an array:
arr=[5,2,1,9,9,7]
n=len(arr)
sum=0
for i in range(0,n):
sum=sum+arr[i]
print("Sum of all the elements of an array: ")
print(sum)
Output:
Sum of all the elements of an array:
33
13.Sort the elements of an array in ascending order:
arr=[5,2,1,9,9,7]
n=len(arr)
temp=0
for i in range(0,n):
for j in range(i+1,n):
if arr[i]>arr[j]:
temp=arr[i]
arr[i]=arr[j]
arr[j]=temp
print("Elements of array sorted in ascending order: ")
for i in range(0,n):
print(arr[i],end=" ")
Output:
Elements of array sorted in ascending order:
1 2 5 7 9 9
14.Sort the elements of an array in descending order:
arr=[5,2,1,9,9,7]
n=len(arr)
temp=0
for i in range(0,n):
for j in range(i+1,n):
if arr[i]<arr[j]:
temp=arr[i]
arr[i]=arr[j]
arr[j]=temp
print("Elements of array sorted in descending order: ")
for i in range(0,n):
print(arr[i],end=" ")
Output:
Elements of array sorted in descending order:
9 9 7 5 2 1