const median = (arr: Array<number>) => {
        const mid: number = Math.floor(arr.length / 2),
          nums: Array<number> = [...arr].sort((a, b) => a - b);
        return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
      };
      
      Typescript language logo

      Calculate median

      Codiga's TypeScript Recipes

      Calculates the median of an array of numbers.

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.