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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:30:11+00:00 2026-06-09T12:30:11+00:00

I have an assignment where I have to create 3 arrays. The first two

  • 0

I have an assignment where I have to create 3 arrays. The first two arrays have similar elements and the third is empty.

const int arraySize = 4;
array k[arraySize] = {1, 2 ,3, 7}
array j[arraySize] = { 1, 2, 8, 9}
array u;

int *ptr1 = arrayk;
    int *ptr2 = arrayj;

How could I compare the elements in the first two and then copy over those duplicates to the third empty array (array u)?

I was thinking something like this:

for(int i = 0; i < arraySize; ++1) {
    for(int k = 0; k < arraySize; ++k) {
        if(&ptr1[i] == &ptr2[k]) {
            //copy elements that are duplicates to array u
          }
      }
}
  • 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-09T12:30:13+00:00Added an answer on June 9, 2026 at 12:30 pm

    Since it looks like homework, I’m assuming you want to do it by yourself not by using a library to achive this. This being the case, your code is fine, you just have to keep an integer variable to store the next available position in u array.

    const int arraySize = 4; 
    int next = 0; 
    array k[arraySize] = {1, 2 ,3, 7};
    array j[arraySize] = { 1, 2, 8, 9};
    array u[arraySize]; // Because it at most be a copy of array k or j
    
    for(int i = 0; i < arraySize; ++i) {
        for(int k = 0; k < arraySize; ++k) {
            if(arrayk[i] == arrayj[k]) {
                u[next++] = arrayk[i];
            }
        }
    }
    

    This way, when you find a duplicate you assign it to the next available position on u and then increase the value of next by one.

    Here is the pointer version, although I strongly recommend to avoid them in cases like this, and use them only when they are needed.

    const int arraySize = 4; 
    int next = 0; 
    array k[arraySize] = {1, 2 ,3, 7};
    array j[arraySize] = { 1, 2, 8, 9};
    array u[arraySize]; // Because it at most be a copy of array k or j
    
    int *ptr1 = arrayk;
    int *ptr2 = arrayj;
    
    for(int i = 0; i < arraySize; ++i) {
        for(int k = 0; k < arraySize; ++k) {
            if(*(ptr1 + i)  == *(ptr2 + k) {
                u[next++] = *(ptr1 + i);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small assignment in C. I am trying to create an array
I have an assignment that I have to create a randomly sized 3D array,
I have an assignment to create an algorithm to find duplicates in an array
I have an assignment to create a GUI using MATLAB GUIDE and am having
I have an assignment to create a secure communication between 2 people with a
For a school assignment I have to create a C++ program that will create
For my second programming assignment in my Java class, we have to create a
I have two basic Cpp tasks, but still I have problems with them. First
Okay, so I have a little assignment to do which needs some array of
I have to create a CD inventory program for my first Java class. The

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.