Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1/50] Median of two sorted arrays of different sizes #34

Open
lpatmo opened this issue Mar 28, 2019 · 1 comment
Open

[1/50] Median of two sorted arrays of different sizes #34

lpatmo opened this issue Mar 28, 2019 · 1 comment

Comments

@lpatmo
Copy link
Member

lpatmo commented Mar 28, 2019

https://leetcode.com/problems/median-of-two-sorted-arrays/

@Dan-Y-Ko
Copy link
Member

Dan-Y-Ko commented Mar 28, 2019

const findMedian = (arr1, arr2) => {
  const newArr = [...arr1, ...arr2];
  newArr.sort((a,b) => a-b);

  const mid = Math.floor(newArr.length / 2)
  let median;

  if (newArr.length === 0) return undefined;

  if (newArr.length % 2 === 0) {
    median = (newArr[mid] + newArr[mid-1]) / 2
    return median;
  } else {
    return newArr[mid];
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants