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

The Archive Base Latest Questions

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

I need to find the duplicate elements in a two dimensional array. route_ptr->route[0][1] =

  • 0

I need to find the duplicate elements in a two dimensional array.

route_ptr->route[0][1] = 24;
route_ptr->route[0][2] = 18;
route_ptr->route[1][1] = 25;
route_ptr->route[2][1] = 18;
route_ptr->route[3][1] = 26;
route_ptr->route[3][2] = 19;
route_ptr->route[4][1] = 25;
route_ptr->route[4][2] = 84;

Those are my data; the duplicate entries of route[2][1] (duplicate of route[0][2]) and route[4][1] (duplicate of route[1][1]) has to be found.

The solution is the duplicate ‘i’ value of route[i][j] which is 2 & 4 from this example.

please guide me.

#include <stdio.h>

struct route{
    int route[6][6];
    int no_routes_found;
    int count_each_route[6];
};

int main() {
    struct route *route_ptr, route_store;  
    route_ptr=&route_store;

    int i,j,k;

    // the data
    route_ptr->route[0][1] = 24;
    route_ptr->route[0][2] = 18;
    route_ptr->route[1][1] = 25;
    route_ptr->route[2][1] = 18;
    route_ptr->route[3][1] = 26;
    route_ptr->route[3][2] = 19;
    route_ptr->route[4][1] = 25;
    route_ptr->route[4][2] = 84;
    route_ptr->count_each_route[0]=3;
    route_ptr->count_each_route[1]=2;
    route_ptr->count_each_route[2]=2;
    route_ptr->count_each_route[3]=3;
    route_ptr->count_each_route[4]=3;
    route_ptr->no_routes_found=5;

    ////  process
    for (i = 0; i <(route_ptr->no_routes_found) ; i++)
    {
        for (j = 1; j < route_ptr->count_each_route[i]; j++)
        {
            printf("\nroute[%d][%d] = ", i, j);
            printf("%d",route_ptr->route[i][j]);
        }
    }
}

The solution expected is:

route[0][1] is compared by route [0][2] i.e [24 !=18]
route[0][1] and route [0][2] is compared by route[1][1] i.e [24 && 18 !=25]
route[0][1] and route[0][2] and route[1][1] is compared by route[2][1] i.e [ 24&&18&&25 is compared by 18, there is a matching element,
   save the newcomer 'i' value which matches to the existence and drop it for next checking]
   break the 'i' loop
route[0][1], route[0][2], route[1][1] is now compared route[3][1]
route[0][1], route[0][2], route[1][1] ,[3][1] is now compared route[3][2]
route[0][1], route[0][2], route[1][1] ,[3][1] ,[3][2] is now compared to route [4][1] i.e [ now there is a match to route[1][1], so save the newcomer 'i' value and  break the 'i' loop

So i values [2 and 4] are duplicate, and that is my expected result of my 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-19T04:40:22+00:00Added an answer on June 19, 2026 at 4:40 am

    Got something against index zero, zero?

    I also don’t see the point of the pointer shenanigans.

    It’s a general safety thing to initialize all your data. You know, to zero or something.

    The algorithm you suggest in your solution is rather hard to be faithful to, but this will find your duplicates. You have to walk through the entire array, in both dimensions, twice.

    This will also match all the zeroes in your data, so you could add an exception to ignore routes values of zero.

    //Cycling through the array the first time.
    for (i = 0; i < 6 ; i++)
    {
        for (j = 0; j < 6; j++)
        {
            //Cycling through the array the second time
            for (x = 0; x < 6 ; x++)
            {
                for (y = 0; y < 6; y++)
                {
                   if(i==x && j==y)
                       continue;
                   if(routestore.route[i][j] == routestore.route[x][y])
                       printf("You have a match [%d][%d] =  [%d][%d]", i, j, x,y);
                }
            }
        }
    }
    

    Ok, so if you only want to see matches once, ie [0][2] == [2][1] but not [2][1] == [0][2], then you can do something like what I have below. This one made me scratch my head. Usually, when it’s a simple list of items, you initialize the inner loop to the value of the outer loop, plus one. But you can’t quite do that when it’s a 2D array. So I gave up and made a super-lame hack-job. I’m a big fan of brute forcing things when possible. I’d normally tell you not to use pointers like this.

    Now… this will still have multiple hits if you have three similar values. If that irks you then you need to start building a list and comparing hits against that as you walk through the data.

    #include <stdio.h>
    #include <string.h>
    
    struct route{
        int route[6][6];
        int no_routes_found;
        int count_each_route[6];
    };
    
    int lameAddOneAlternative(int *i, int *j)
    {
      if((*j)<6)
      {
        (*j)++;
        return 1;
      }
      else if (*i<6)
      {
        (*i)++;
        (*j) = 0;
        return 1;
      }  
      return 0;
    }
    
    int main(int argc, char **argv)
    {
      struct route routeStore;  
      int i,j,x,y;
    
      memset(routeStore.route,0,sizeof(int)*36);
    
      // the data
      routeStore.route[0][1] = 24;
      routeStore.route[0][2] = 18;
      routeStore.route[1][1] = 25;
      routeStore.route[2][1] = 18;
      routeStore.route[3][1] = 26;
      routeStore.route[3][2] = 19;
      routeStore.route[4][1] = 25;
      routeStore.route[4][2] = 84;
    
      //Cycling through the array the first time.
      for (i = 0; i < 6 ; i++)
      {
        for (j = 0; j < 6; j++)
        {
          x=i;
          y=j;
          //Cycling through the array the second time
          while(lameAddOneAlternative(&x,&y))
          {
            if(routeStore.route[i][j] == 0 )
              continue;
            if(routeStore.route[i][j] == routeStore.route[x][y])
              printf("You have a match [%d][%d], [%d][%d] == %d\n", i, j, x,y, routeStore.route[i][j] );
    
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to create sql procedure or function that need to find duplicate
I need to write a query that will go through a table, find duplicate
Possible Duplicate: JSON Parsing in Android As I find out I need to use
I need to find a flexible solution for access control in PHP. In the
I have an array of nearly sorted values 28 elements long. I need to
My requirement is to find a duplicate number in an array of integers of
I need to compare 2 tables to find a duplicate, but the fields allow
Possible Duplicate: Circle line collision detection I have a problem. I need to find
I need to find the n largest elements in a list of tuples. Here
I need to find the duplicate values in a perl hash and then output

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.