xxxxxxxxxx
#Initialize array
arr = [25, 11, 7, 75, 56];
#Initialize max with first element of array.
max = arr[0];
#Loop through the array
for i in range(0, len(arr)):
#Compare elements of array with max
if(arr[i] > max):
max = arr[i];
print("Largest element present in given array: " + str(max));
xxxxxxxxxx
// you can just use the max function on an array to find the max
arr = [1, 7, 3, 9]
max(arr) // returns 9 because it is the largest
xxxxxxxxxx
def find_kth_largest(array, k):
# Sort the array in descending order
array.sort(reverse=True)
# Return the kth largest element if it exists
if k <= len(array):
return array[k - 1]
# Return None if k is out of the array's range
return None
# Example usage:
arr = [5, 8, 2, 10, 3]
k = 3
print(find_kth_largest(arr, k)) # Output: 5