Python Array Programs
1) Python program to copy all elements of one array into another array
In this program, we need to copy all the elements of one array into another. This can be accomplished by looping through the first array and store the elements of the first array into the second array at the corresponding position.
ARRAY 1
1 2 3 4 5
ARRAY 2
1 2 3 4 5
ALGORITHM:
-
STEP 1: Declare and initialize an array.
-
STEP 2: Declare another array of the same size as of the first one
-
STEP 3: Loop through the first array from 0 to length of the array and copy an element from the first array to the second array that is arr1[i] = arr2[i].
#Initialize array
arr1 = [1, 2, 3, 4, 5];
#Create another array arr2 with size of arr1
arr2 = [None] * len(arr1);
#Copying all elements of one array into another
for i in range(0, len(arr1)):
arr2[i] = arr1[i];
#Displaying elements of array arr1
print("Elements of original array: ");
for i in range(0, len(arr1)):
print(arr1[i]),
print();
#Displaying elements of array arr2
print("Elements of new array: ");
for i in range(0, len(arr2)):
print(arr2[i]),
2) Python program to find the frequency of each element in the array
In this program, we have an array of elements to count the occurrence of its each element. One of the approaches to resolve this problem is to maintain one array to store the counts of each element of the array. Loop through the array and count the occurrence of each element as frequency and store it in another array fr.
1 2 8 3 2 2 2 5 1
In the given array, 1 has appeared two times, so its frequency is 2, and 2 has appeared four times so have frequency 4 and so on.
ALGORITHM:
-
STEP 1: Declare and initialize an array arr.
-
STEP 2: Declare another array fr with the same size of array arr. It is used to store the frequencies of elements present in the array.
-
STEP 3: Variable visited will be initialized with the value -1. It is required to mark an element visited that is, it helps us to avoid counting the same element again.
-
STEP 4: The frequency of an element can be counted using two loops. One loop will be used to select an element from an array, and another loop will be used to compare the selected element with the rest of the array.
-
STEP 5: Initialize count to 1 in the first loop to maintain a count of each element. Increment its value by 1 if a duplicate element is found in the second loop since we have counted this element and didn't want to count it again. Mark this element as visited by setting fr[j] = visited. Store count of each element to fr.
-
STEP 6: Finally, print out the element along with its frequency.
#Initialize array
arr = [1, 2, 8, 3, 2, 2, 2, 5, 1];
#Array fr will store frequencies of element
fr = [None] * len(arr);
visited = -1;
for i in range(0, len(arr)):
count = 1;
for j in range(i+1, len(arr)):
if(arr[i] == arr[j]):
count = count + 1;
#To avoid counting same element again
fr[j] = visited;
if(fr[i] != visited):
fr[i] = count;
#Displays the frequency of each element present in array
print("---------------------");
print(" Element | Frequency");
print("---------------------");
for i in range(0, len(fr)):
if(fr[i] != visited):
print(" " + str(arr[i]) + " | " + str(fr[i]));
print("---------------------");