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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:21:54+00:00 2026-06-10T04:21:54+00:00

I would like to compare each element of one array to all elements of

  • 0

I would like to compare each element of one array to all elements of another array. What I want to achieve is if an element exists in another array, result =0, otherwise result =1

int m,n;

for(int i=0; i<m; i++) {
  for(int j=0; j<n; j++) {
    if(i==j) {
      result =0;
      //perform a task
       break;
    } 

     if(i!=j) {
       result = 1;
      //perform another task
      break
     }

  }
}

however, i fail to achieve what i want in the second if ()

  • 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-10T04:21:56+00:00Added an answer on June 10, 2026 at 4:21 am

    Tweaking your code somewhat (replace char with whatever datatype you’re actually using):

    char A[50];
    char B[50];
    
    for(int i=0; i<50; i++) {     // Iterate through A from 0 to 50
      for(int j=0; j<50; j++) {   // Iterate through B from 0 to 50
        if(A[i] == B[j]) {
           // Item from A exists in B
        }
        else {
           // Item from A does not exist in B
        }
      }
    }
    

    Note that the “else” code will run once for every element.

    We can do better. First make a utility function that searches an array:

    bool item_exists(char item, char[] array, int array_len) {
        for (int i=0; i<array_len; i++) {
           if (array[i] == item)
              return true;
        }
        return false;
    }
    

    Then use it.

    char A[50];
    char B[50];
    
    for(int i=0; i<50; i++) {
        if (item_exists(A[i], B, 50)) {
           // Item from A exists in B
        }
        else {
           // Item from A does not exist in B
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to compare 2 images similarity with percentage. I want to detect
I would like to compare two columns in the same table. I want to
I have two files that I would like to compare, however they are each
I would like to write an algorithm that compares one memory block with another
On my website I would like to compare two companies with an image-chart/-graph. Each
I'm planning on read a *.cpp file and I would like to compare each
I would like to do something if one modeladmin is passed and do another
I would like to compare a date entered in a search field (e.g., 04.07.2012)
I would like to compare 4 character string using wildcards. For example: std::string wildcards[]=
I would like to compare or monitor a sql file in the Database of

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.