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

  • Home
  • SEARCH
  • 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 7516625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:11:27+00:00 2026-05-30T01:11:27+00:00

What’s a fastest way to implement array subtraction? For example: array a1 = [1,

  • 0

What’s a fastest way to implement array subtraction? For example:

array a1 = [1, 3, 4, 5, 8];
array a2 = [2, 4, 5];

array a3 = a1 - a2; /* [1, 3, 8] */

Here array would be the type my program uses to represent a struct which is used as a container. The rest of it is pseudo code, of course I’m not creating the arrays like that nor subtracting.

The simplest solution I can think of involves nested loops:

/* a1 - a2 */
for (i = 0; i < a1.size; ++i) {
    int is_the_same = 0;
    for (j = 0; i < a2.size; ++j)
        if (a1[i] == a2[j]) {
            is_the_same = 1;
            break;
        }
    }
    if (!is_the_same)
       a3.push a1[i];
}

But this does not look very efficient. What would be another approach?

  • 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-30T01:11:30+00:00Added an answer on May 30, 2026 at 1:11 am

    If your arrays aren’t sorted, the worst case time complexity for an array exclusion using a intuitive solution is O(n2) (although you can boost this if you sort the arrays first), since you need to check the whole array whether an element is existent or not.

    Example of worst case scenario:

    array a1 = [1, 3, 4, 5, 8];
    array a2 = [8, 5, 4, 3, 1];
    

    If your arrays are ordered, then the worst case time complexity is O(n+m) (pseudo-code):

    int i = 0;
    for(int j = 0; i < a1.size && j < a2.size;){
        if(a1[i] == a2[j])
            ++i, ++j;  // exclude this element
        if(a1[i] < a2[j]){
             a3.push(a1[i]); // include this element
             ++i;
        }
        if(a1[i] > a2[j])
             ++j; // ignore lesser elements
    }
    while(i < a1.size)
         a3.push(a1[i]);
    

    UPDATE -Wall -Wextra -pedantic C code:

    #include <stdio.h>
    #include <malloc.h>
    
    /**
    * The following function excludes values from an array using another arrays values.
    * Note that this version won't exclude multiple values, for this you have to drop
    * '++j' in line 25.
    *
    * \param[in] from Original sorted array
    * \param[in] from_length Original array length
    * \param[in] what Sorted array including the excluding values
    * \param[in] what_length self describing
    * \param[out] result_length the lenght of the new array - a value lesser 0 indicates an error.
    */
    
    int* exclude(int* from, int from_length, int* what, int what_length, int* result_length){
        int i,j,k;
        int* result = (int*) malloc(sizeof(int)*from_length);
        if(result == NULL){
            *result_length = -1;
            return NULL;
        }
        for(i = j = k = 0; i < from_length && j < what_length;){
            if(from[i] == what[j])
                ++i, ++j;  /* exclude this element - to enable multiple exclusion drop '++j' 
                            4,4,5,6 /4 --> 5,6 */
            if(from[i] < what[j])
                result[k++] = from[i++];
            if(from[i] > what[j])
                 ++j; /* ignore lesser elements */
        }
        while(i < from_length)
            result[k++] = from[i++];
    
        if( k < from_length){
            int* tmp = (int*) realloc(result,sizeof(int)*k);
            if(tmp == NULL){
                /* either error handling or returning result */
            }else{
                result = tmp;
            }
        }
        *result_length = k;
        return result;
    }
    
    int main(){
        int a[6] = {1,2,3,4,5,6};
        int b[3] = {2,4,5};
        int result_length;
        int i;
        int *c = exclude(a,6,b,3,&result_length);
        for(i = 0; i < result_length; ++i)
            printf("%i ",c[i]);
        free(c);
        return 0;
    }
    

    This will result in a worst time complexity of O(n+m) for sorted arrays and O(n log n + m log m) for non-sorted arrays (sort both, use the function provided above).

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

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string

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.