Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Find the smallest element in an array

#find the smallest element in an array
#Approach: 
#Sort the array in ascending order.
arr = [2,5,1,3,0]


# sorted method
my_arr = sorted(arr)
print(f"The smallest number is {my_arr[0]}")

#* USING FOR LOOP 
smallest = arr[0]
for i in range(0,len(arr)):
    if arr[i] < smallest :
        smallest = arr[i]
print(f"The smallest number is {smallest}")
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Find #smallest #element #array
ADD COMMENT
Topic
Name
4+8 =