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 6868623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:27:25+00:00 2026-05-27T03:27:25+00:00

I have been programming in PHP for a long time now, and as I

  • 0

I have been programming in PHP for a long time now, and as I don’t come from a computer science / math background I only have a basic understanding of the Big O notation, so I have taken a function and tried to improve it and express them both in Big O:

Example 1

function foo($j) {
    $ids = array();
    while (count($ids) < $j) {
        $id = gen_id();  // assume this function will return you an integer
        if (!in_array($id, $ids)) {
            $ids[] = $id;
        }
    }
    return $ids;
}

I would improve this function by removing the while loop for count($ids) and replace with forloop So that count($ids) doesn’t have to be evaluated every time in the loop, and also replace is_array with isset and move the values to keys (isset is quicker than in_array)

Example 2 – Improved

function foo($j) {
    $ids = array();
    for($i = 1; $i<= $j; ++$j) {
        $id = gen_id();  // assume this function will return you an integer
        if (!isset($ids[$id])) {
            $ids[$id] = true;
        }
        else {
            --$i;
        }
    }
    return $ids;
}

I would express the above functions with the following Big O Notation:

Example 1: O(n^2), Example 2: O(n)

Am I correct with my notations? Is there a better way still to do the same function?

Thanks in advance!

  • 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-05-27T03:27:26+00:00Added an answer on May 27, 2026 at 3:27 am

    Here is how I would write that function in a more straightforward approach: The inner for loop is the perfect scenario for a do ... while loop.

    function foo( $j)
    {
        $ids = array();
        for( $i = 0; $i < $j; $i++)
        {
            do
            {
                $id = gen_id();
            } while( isset( $ids[$id]));
            $ids[$id] = true;
        }
        return $ids;
    }
    

    Edit: Actually, this function is O($j) / O(n), since gen_id() and isset() are both constant O(1) (isset() is a hash table lookup). So, the inner efficiency of the for loop is constant time O(1), making the whole function O(n).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been programming in PHP and C# for a long time, but I
I have been programming in Perl, off and on, for years now, although only
I've been programming PHP for a long time, but not so much PHP 5...
Since some time ago I have been programming in php with oracle, and I've
I've been programming in PHP for several years now, but I also have some
Hey I am very new to Web Programming. I have been learning PHP from
I have been programming using PHP, Javascript and have been a designer for a
I have been programming in Python, PHP, Java and C for a couple or
I have been programming in C and C++ for a few years and now
I have been programming procedural PHP for about 8 years, and am finally starting

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.