Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8216765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:14:40+00:00 2026-06-07T12:14:40+00:00

I write this function for merging two arrays. private static int[] Merge(int[] array1, int[]

  • 0

I write this function for merging two arrays.

private static int[] Merge(int[] array1, int[] array2)
{
    var mergedArray = new int[array1.Length + array2.Length];
    int i = 0, j = 0, k = 0;
    while(k < mergedArray.Length)
    {
        if(i == array1.Length || j == array2.Length)
        {
             if (i <= j)
                {
                    mergedArray[k] = array1[i];
                    i++;
                }
                else
                {
                    mergedArray[k] = array2[j];
                    j++;
                }
        }
        else
        {
            if(array1[i] < array2[j])
            {
                mergedArray[k] = array1[i];
                i++;
            }
            else
            {
                mergedArray[k] = array2[j];
                j++;
            }
        }
        k++;
    }
    return mergedArray;
}

How to reduce if statements in this code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:14:43+00:00Added an answer on June 7, 2026 at 12:14 pm

    Not as good as the Linq solution, but if you want the traditional if-then style function you could write:

    private static int[] Merge(int[] array1, int[] array2)
    {
        var mergedArray = new int[array1.Length + array2.Length];
        int i = 0, j = 0, k = 0;
        while(k < mergedArray.Length)
        {
            if (j == array2.Length || ((i < array1.Length) && (array[i] < array2[j])))
            {
                mergedArray[k] = array1[i];
                i++;
            }
            else
            {
                mergedArray[k] = array2[j];
                j++;
            }
            k++;
        }
        return mergedArray;
    }
    

    (edit: missing brace added)

    Or in English:

    If array2 is empty or if there are still values in array 1 and array1[i] is less than array2[j], then take value from array1, otherwise take from array 2

    Or very concise (just for fun):

    private static int[] Merge(int[] array1, int[] array2)
    {
        var mergedArray = new int[array1.Length + array2.Length];
        int i = 0, j = 0;
        while(i+j < mergedArray.Length)
            if (j == array2.Length || ((i < array1.Length) && (array1[i] < array2[j])))
                mergedArray[i+j] = array1[i++];
            else
                mergedArray[i+j] = array2[j++];
        return mergedArray;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I want to write a function like this: int get_some_int(string index) { ...perform
I'd like to write this function: function getResults(nums){ var results = []; for(var i
Say I write this function... var sayHi = function() { return hi; } alert(sayHi());
I have this function - public int GetAvgResult() { var weeklyvalues=GetWeeklyValues();//gets list of weekly
i tried to write this function: http://uggedal.com/reddit.cf.algorithm.png in javascript: function getRating(t,u,d){ var x =
I write this function, but what's its return value. (defn read-data [file] (let [code
It is valid JavaScript to write something like this: function example(x) { Here is
I am a Ruby newbie. How can I write better for this function? can
I'm trying to write a calendar function like this function get_date($month, $year, $week, $day,
So I know I can write my own HTML-encoding function like this: function getHTMLEncode(t)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.