Code This #5: Find Min and Max In An Array

Interview Question #5:

Write a function that will return the min and max numbers in an array.?❓

If you need practice, try to solve this on your own. I have included 3 potential solutions below.

Note: There are many other potential soluti…


This content originally appeared on DEV Community and was authored by Let's Code

Interview Question #5:

Write a function that will return the min and max numbers in an array.?❓

If you need practice, try to solve this on your own. I have included 3 potential solutions below.

Note: There are many other potential solutions to this problem.

Feel free to bookmark ? even if you don't need this for now. You may need to refresh/review down the road when it is time for you to look for a new role.

Code: https://codepen.io/angelo_jin/pen/zYzvQdM

Solution #1: Math Methods - min and max

  • Spread the array to Math methods like below and we are set
function getMinMax(arr) {
  return {
    min: Math.min( ...arr ),
    max: Math.max( ...arr )
  }
}

Solution #2: Array Sort

  • Sort the array first using an efficient merging algorithm of choice. Once sorting is done, the first element would be the minimum and the last would be the maximum.
function getMinMax(arr) {
  const sortedArray = arr.sort((a, b) => a - b)

  return {
    min: sortedArray[0],
    max: sortedArray[sortedArray.length - 1]
  }
}

Solution #3: for of loop

  • Below solution will use two variables and will compare each array elements and assign it to min and max if it meets the condition accordingly.
function getMinMax(arr) {
  let min = arr[0];
  let max = arr[0];

  for (let curr of arr) {
    if (curr > max) {
      max = curr;
    }

    if (curr < min) {
      min = curr;
    }
  }

  return {
    min,
    max
  };
}

In case you like a video instead of bunch of code ??

Happy coding and good luck if you are interviewing!

If you want to support me - Buy Me A Coffee


This content originally appeared on DEV Community and was authored by Let's Code


Print Share Comment Cite Upload Translate Updates
APA

Let's Code | Sciencx (2021-09-01T19:42:16+00:00) Code This #5: Find Min and Max In An Array. Retrieved from https://www.scien.cx/2021/09/01/code-this-5-find-min-and-max-in-an-array/

MLA
" » Code This #5: Find Min and Max In An Array." Let's Code | Sciencx - Wednesday September 1, 2021, https://www.scien.cx/2021/09/01/code-this-5-find-min-and-max-in-an-array/
HARVARD
Let's Code | Sciencx Wednesday September 1, 2021 » Code This #5: Find Min and Max In An Array., viewed ,<https://www.scien.cx/2021/09/01/code-this-5-find-min-and-max-in-an-array/>
VANCOUVER
Let's Code | Sciencx - » Code This #5: Find Min and Max In An Array. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/01/code-this-5-find-min-and-max-in-an-array/
CHICAGO
" » Code This #5: Find Min and Max In An Array." Let's Code | Sciencx - Accessed . https://www.scien.cx/2021/09/01/code-this-5-find-min-and-max-in-an-array/
IEEE
" » Code This #5: Find Min and Max In An Array." Let's Code | Sciencx [Online]. Available: https://www.scien.cx/2021/09/01/code-this-5-find-min-and-max-in-an-array/. [Accessed: ]
rf:citation
» Code This #5: Find Min and Max In An Array | Let's Code | Sciencx | https://www.scien.cx/2021/09/01/code-this-5-find-min-and-max-in-an-array/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.