#removes adjacent duplicate in an Array

48 messages · Page 1 of 1 (latest)

dire sorrelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

rugged blade
#

I would love some advice on how to get this to work. Is it possible to remove an element from an array?

quartz lagoon
#

BRUH

#

are you going for efficiency or just getting the code done quickly

rugged blade
#

Can you stop?

#

I'm allowed to ask for help

quartz lagoon
#

are you trying to make the program efficient or are you trying to get it done quickly

#

if you're going for efficiency you shouldn't use strcmp, you should use a hash function

rugged blade
#

I'm going to keep strcmp but i want to know if it's possible to remove an element from an array?

quartz lagoon
#

yes it is

rugged blade
#

And what function or method does that?

quartz lagoon
#

you should create the function yourself. what you're going to need to do is assign the ith elment of the array to the value of the last element of the array, then use realloc() to reallocate the array to one element of size smaller

rugged blade
#

realloc() is not something we have gone through on the course. Is there any other method?

grim robin
#

strcmp in this case won't work, as you are trying to compare characters and not strings, you have to just use a normal comparison

rugged blade
#

Aah that makes sense!

#
array[i] == array[i + 1]```
#

So something like this?

grim robin
#

yes

rugged blade
#

I had gpt help me

#
// 2023 Alexander Johansson
#include <stdio.h>
#include <string.h>

int main()
{
    char array[60];
    while (scanf("%59s", array) == 1)
    {
        int len = strlen(array);
        for (int i = 0; i < len - 1; i++)
        {
            if (array[i] == array[i + 1])
            {
                for (int j = i + 1; j < len; j++)
                {
                    array[j] = array[j + 1];
                }
                len--;
                i--;
            }
        }
        printf("%s\n", array);
    }
}
#

I'm just a little confused on what the last for loop does

for (int j = i + 1; j < len; j++)
                {
                    array[j] = array[j + 1];
                }
quartz lagoon
#

that should work

quartz lagoon
grim robin
# rugged blade I would love some advice on how to get this to work. Is it possible to remove an...

you can visualize removing an element from an array as looping over the elements to it's right and copying them by 1 to the left

[51] [53] [12] [74] [10]
      XX

[51] [12] [74] [10] [10]

you just have to implement such thing by yourself, but we can of course help if you feel stuck
(of course you'll need to handle the last element being there twice, but you can decide for yourself how to do such thing)

rugged blade
#

So if the word that is put in is e.g. Programming

quartz lagoon
#

then programing

rugged blade
#

It will make the second m become an i?

#

and then the i becomes an n

quartz lagoon
#

yes

rugged blade
#

and then the n becomes a g?

#

aaah

#

that's clever

grim robin
#

yeah

rugged blade
#

But what happens with the final 2 dublicate g:s?

quartz lagoon
#

what do you mean

#

in the word programming?

rugged blade
#

Programming

Yes if i have this, at the end i will have dublicates g:s

quartz lagoon
#

no you won't

#

why would you have duplicate g's

#

the g is assigned to the null terminator

#

after the n is assigned to the g

grim robin
rugged blade
#

Ah yeah i see

rugged blade
#

!solved

dire sorrelBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

quartz lagoon
#

you know that's funny cause it looks like not solved