Hello.

I have encountered a following problem "find all duplicate letters in a 2d char array and replace them with '@'"
So for instance if I have this :
a a
b c

The output would be
@ @
b c

So. I have come up with the following algorithm

``````/*Up there are standard 2d array memory allocation procedure with malloc, manual array filler and a array printer*/
char **ptr=arr;             //this will point to the given element

for(i=MIN;i<row;i++)
{
for(j=MIN;j<col;j++)
{
buff=arr[i][j];        //seed the element

for(k=MIN;k<row;k++)
{
for(l=MIN;l<col;l++)
{
if(buff==arr[k][l])    //check occurrences
{
cnt++;             //if any, increment
}
if(cnt>1)              //if more than one
{
*letter_occurrence=arr[k][l];   //point out that element
}
else
{
continue;
}
}
}
}
}
//then the replacement procedure
for(i=MIN;i<row;i++)
{
for(j=MIN;j<col;j++)
{
if(arr[i][j]==*letter_occurrence)
{
arr[i][j]='@';
}
printf("%c\t",arr[i][j]);
}
printf("\n");
}``````

The problem with this is that well it replaces only the last letter occurrence
Input :
a a
b b

Output
a a
@ @

Thank you very much for any kind of input !

Your representation of your 2-dim array leaves a lot open for interpretation...Do you mean if you have a 2-dim array like below

{a, b, c, d}{a, f, h, k}

would have a final result

{@, b, c, d}{@, f, h, k}

Go through the array twice:

First time, count the number of times each letter appears. That's easily done using logic like this:

``````for each row
for each columm
ascii[this letter's ascii value]++, coded like:
ascii[myLetter]++;
end for
end for``````

(a 256 element array of type int, will do …

All 8 Replies

Your representation of your 2-dim array leaves a lot open for interpretation...Do you mean if you have a 2-dim array like below

{a, b, c, d}{a, f, h, k}

would have a final result

{@, b, c, d}{@, f, h, k}

Your representation of your 2-dim array leaves a lot open for interpretation...Do you mean if you have a 2-dim array like below

{a, b, c, d}{a, f, h, k}

would have a final result

{@, b, c, d}{@, f, h, k}

Yes, that is correct.

I'm still a little foggy on the rules for replacing your characters with '@'...What if you had a scenario like below.

``````#include <stdio.h>
#include <stdbool.h>

char ch[2][4] = {{'a', 'a', 'c', 'a'}, {'e', 'f', 'c', 'h'}};

void find_match(char *a , int row, int cols)
{
int i = 0;
int j = 0;
bool foundit = false;

for (; i < (row * cols); ++i)
{
for (j = i + 1; j < (row * cols); ++j)
{
if (a[i] == a[j])
{
foundit = true;
a[j] = '@';
}
}
if (foundit)
{
foundit = false;
a[i] = '@';
}
}
}

int main()
{
int i = 0, j = 0;
find_match((char*)ch, 2, 4);

for (; i < 2; ++i)
{
for (j = 0; j < 4; ++j)
{
fprintf(stdout, "[%d][%d]->%c\n", i, j, ch[i][j]);
}
}

return 0;
}``````

output

[0][0]->@
[0][1]->@
[0][2]->@
[0][3]->@
[1][0]->e
[1][1]->f
[1][2]->@
[1][3]->h

I'm still a little foggy on the rules for replacing your characters with '@'...What if you had a scenario like below.

``````#include <stdio.h>
#include <stdbool.h>

char ch[2][4] = {{'a', 'a', 'c', 'a'}, {'e', 'f', 'c', 'h'}};

void find_match(char *a , int row, int cols)
{
int i = 0;
int j = 0;
bool foundit = false;

for (; i < (row * cols); ++i)
{
for (j = i + 1; j < (row * cols); ++j)
{
if (a[i] == a[j])
{
foundit = true;
a[j] = '@';
}
}
if (foundit)
{
foundit = false;
a[i] = '@';
}
}
}

int main()
{
int i = 0, j = 0;
find_match((char*)ch, 2, 4);

for (; i < 2; ++i)
{
for (j = 0; j < 4; ++j)
{
fprintf(stdout, "[%d][%d]->%c\n", i, j, ch[i][j]);
}
}

return 0;
}``````

output

[0][0]->@
[0][1]->@
[0][2]->@
[0][3]->@
[1][0]->e
[1][1]->f
[1][2]->@
[1][3]->h

In this case only the letter 'a' would be replaced because it is most common. I think Ive made an haste-type-error while writing a problem, it should state replace the letter which is most common in that 2d array.
My problem is if I have for example multiple letters that occur the same number of times, how to replace all of them, and not only the last occurrence.
So the output should be : [a matrix type view]

Input:
a b c
d e f
a b g
h a b

Output:
@ @ c
d e f
@ @ g
h @ @

@Joey_Brown

just want to understand your question...
what would be the output of your program if input is this??

Input

a b c
d e b
c b a
d f c

output
??

@Joey_Brown

just want to understand your question...
what would be the output of your program if input is this??

Input

a b c
d e b
c b a
d f c

output
??

In this case letters c and b occur three times, so the output would be
like this.

a @ @
d e @
@ @ a
d f @

Go through the array twice:

First time, count the number of times each letter appears. That's easily done using logic like this:

``````for each row
for each columm
ascii[this letter's ascii value]++, coded like:
ascii[myLetter]++;
end for
end for``````

(a 256 element array of type int, will do nicely for ascii[].)

Now when complete, go through the ascii[] array, and find the largest values(s), and assign them to your maxLetter1, maxLetter2, etc., variables.

Then go back through your array, and mark out the letter which equal maxLetter1, or maxLetter1 and maxLetter2, etc.

Sounds like it would take a long time, but it doesn't because the small array easily fits in cache memory -- Zoom! ;)

Go through the array twice:

First time, count the number of times each letter appears. That's easily done using logic like this:

``````for each row
for each columm
ascii[this letter's ascii value]++, coded like:
ascii[myLetter]++;
end for
end for``````

(a 256 element array of type int, will do nicely for ascii[].)

Now when complete, go through the ascii[] array, and find the largest values(s), and assign them to your maxLetter1, maxLetter2, etc., variables.

Then go back through your array, and mark out the letter which equal maxLetter1, or maxLetter1 and maxLetter2, etc.

Sounds like it would take a long time, but it doesn't because the small array easily fits in cache memory -- Zoom! ;)

Thank you Adak, I will implement, and get back !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.