Finding Minimum and Maximum in an Array

In this task, I worked on finding the minimum and maximum values from an array. Instead of writing everything in one place, I used a function to make the code cleaner and reusable. What I Did I cre...

By · · 1 min read
Finding Minimum and Maximum in an Array

Source: DEV Community

In this task, I worked on finding the minimum and maximum values from an array. Instead of writing everything in one place, I used a function to make the code cleaner and reusable. What I Did I created a function called find_min_max that takes an array as input and returns both the smallest and largest values. For example: Input: [1, 4, 3, 5, 8, 6] Output: [1, 8] How I Solved It First, I assumed that the first element of the array is both the minimum and maximum. Then I used a loop to go through each number in the array. While looping: If a number is smaller than the current minimum, I update the minimum If a number is larger than the current maximum, I update the maximum At the end, I return both values as a list. CODE: '''python class Solution: def getMinMax(self, arr): minimum = arr[0] maximum = arr[0] for num in arr[1:]: if num < minimum: minimum = num elif num > maximum: maximum = num return [minimum, maximum]''' How It Works The function checks each element only once. It ke

Related Posts

Similar Topics

#data science (2769)#machine learning (1447)#artificial intelligence (737)#ai (652)#hands on tutorials (454)#data visualization (306)#data engineering (242)#webdev (340)#deep dives (297)#coding (195)#large language models (265)#science and technology (277)#editors pick (267)#software engineering (181)#tutorial (204)#productivity (212)#deep learning (134)#data analysis (173)#pandas (158)#llm (161)

Trending on ShareHub

  1. Understanding Modern JavaScript Frameworks in 2026
    by Alex Chen · Feb 12, 2026 · 0 likes
  2. The System Design Primer
    by Sarah Kim · Feb 12, 2026 · 0 likes
  3. Just shipped my first open-source project!
    by Alex Chen · Feb 12, 2026 · 0 likes
  4. OpenAI Blog
    by Sarah Kim · Feb 12, 2026 · 0 likes
  5. Building Accessible Web Applications: A Practical Guide
    by Alex Chen · Feb 12, 2026 · 0 likes
  6. Rapper Lil Poppa dead at 25, days after releasing new music
    Rapper Lil Poppa dead at 25, days after releasing new music
    by Anonymous User · Feb 19, 2026 · 0 likes
  7. write-for-us
    by Volt Raven · Mar 7, 2026 · 0 likes
  8. Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    by Anonymous User · Feb 12, 2026 · 0 likes
    #coffee gets cold #the #time travel
  9. Best DoorDash Promo Code Reddit Finds for Top Discounts
    Best DoorDash Promo Code Reddit Finds for Top Discounts
    by Anonymous User · Feb 12, 2026 · 0 likes
    #doordash #promo #reddit
  10. Premium SEO Services That Boost Rankings & Revenue | VirtualSEO.Expert
    by Anonymous User · Feb 12, 2026 · 0 likes
  11. NBC under fire for commentary about Team USA women's hockey team
    NBC under fire for commentary about Team USA women's hockey team
    by Anonymous User · Feb 18, 2026 · 0 likes
  12. Where to Watch The Nanny: Streaming and Online Viewing Options
    Where to Watch The Nanny: Streaming and Online Viewing Options
    by Anonymous User · Feb 12, 2026 · 0 likes
    #streaming #the nanny #where
  13. How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    by Anonymous User · Feb 12, 2026 · 0 likes
    #kindle unlimited #subscription #unlimited
  14. Russian skater facing backlash for comment about Amber Glenn
    Russian skater facing backlash for comment about Amber Glenn
    by Anonymous User · Feb 18, 2026 · 0 likes
  15. Google News
    Google News
    by Anonymous User · Feb 18, 2026 · 0 likes

Latest on ShareHub

Browse Topics

#artificial intelligence (36884)#data science (24144)#generative ai (19046)#ai (17855)#crypto (15051)#machine learning (14735)#bitcoin (14333)#featured (13584)#news & insights (13064)#crypto news (11118)

Around the Network