print repeated array of 2D

I need help with Printing duplicate 2D array elements

if for 1D array
1
2
3
4
5
6
7
8
for (int i = 0; i<0; i++){
     for (int j = i+1; j<0; i++)
{
      if (arra[i] == arra[j]){
          cout<<arra[i]<<endl
}
}
}


how would i do for 2D

any help is appreciated

Thank you
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    const int M = 3, N = 5 ;

    int a[M][N] = { { 0, 1, 2, 3, 4, }, { 5, 2, 6, 4, 7 }, { 6, 7, 5, 8, 9 } };
    
    for( int i = 0 ; i < M*N ; ++i )
    {
        for( int j = i+1 ; j < M*N ; ++j )
            if( a[0][i] == a[0][j] ) std::cout << a[0][i] << ' ' ; 
    }
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/945d1fc2dabfa5fa
Thanks, i see what you did, but what if i was doing 10x10 array,
and what if i want to print out the indexes that have the same number as well

for example
if index [1][0] and index[5][8] have the same number,
and i want to print out the position of the indexes and as well as the number
they have in common with
Since we know the extents (number of rows and columns) of the array, the indices can be computed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
    const int NROWS = 3, NCOLS = 5 ;

    int a[NROWS][NCOLS] = { { 0, 1, 2, 3, 4, }, { 5, 2, 6, 4, 7 }, { 6, 7, 5, 8, 4 } };

    for( int i = 0 ; i < NROWS*NCOLS ; ++i )
    {
        const int row = i / NCOLS ;
        const int col = i % NCOLS ;

        for( int j = i+1 ; j < NROWS*NCOLS ; ++j )
        {
            const int row2 = j / NCOLS ;
            const int col2 = j % NCOLS ;
            
            if( a[row][col] == a[row2][col2] )
                std::cout << "a[" << row << "][" << col << "]  " << a[row][col]
                           << "  a[" << row2 << "][" << col2 << "]\n" ;
        }
    }
}

http://coliru.stacked-crooked.com/a/0269d82e42280538
Thanks a lot
it is also to do it in a four nested loops right?
because i did that at first, but wasn't sure if it was correct, but i got similar answer with your code and four nested loops

Thanks again for your help
> it is also to do it in a four nested loops right?

Yes.
Topic archived. No new replies allowed.