2

I am trying to make a function that cleans out my duplicates of number in my array, but it seems that i cant figure out what i am missing to just remove the duplicate. Just to make it more clear: The function should not become void.

[1,2,3,3,4] -> [1,2,3,4]
[4,2,5,1]->[4,2,5,1]
[32,21,2,5,2,1,21,4]->[32,21,2,5,1,4]

It should not be empty spaces in my array, and the function should return the unique elements in the cleaned array, where cleaned is defined as "non-duplication of integer numbers"

#include <stdio.h>

int generateUniqeList(int *list, int length);

int main()
{
    int list[6] = { 5, 5, 4, 3, 2, 1 };
    int duplicate = generateUniqeList(list, 6);

    for (int i = 0; i < 6; i++)
    {
        printf("%d\n", list[i] - 1); //Here i am able to change the value of the duplicates with the - 1
    }
    getchar();

    return 0;
}


int generateUniqeList(int *list, int length)
{
    int duplicate = 0;

        for (int i = 0; i < length; i++)
        {
            if (list[i] == list[i])
                duplicate = list[i];
        }
    return duplicate;
}
7
  • 2
    Do you expect this condition if (list[i] == list[i]) to evaluate to false for some values of i? Commented May 5, 2017 at 15:22
  • 1
    @jhhoff02 this isn't objective c Commented May 5, 2017 at 15:26
  • Do you have an upper bound on the element value? Commented May 5, 2017 at 15:30
  • To remove an int, you will have to shift the whole array an index to the left from the current position that you want to remove. Commented May 5, 2017 at 15:48
  • @Module assuming you wan't to do it in-place.. I would go with generating a new list. Commented May 5, 2017 at 15:50

3 Answers 3

1
#include <stdio.h>
#include<string.h>


void generateUniqeList(int *list, int length);

int main()
{
  int list[6] = { 5, 5, 4, 3, 2, 1 };
  generateUniqeList(list, 6);

  for (int i = 0; i < 6; i++)
  {
    printf("%d", list[i]); 
  //Here i am able to change the value of the duplicates with the - 1
  }
  putchar('\n');

  return 0;
}


void generateUniqeList(int *list, int length){
  int i ,j,k;
  for (i = 0; i < length; i++) {
      for (j = i + 1; j < length;) {
         if (list[j] == list[i]) {
            for (k = j; k < length; k++) {
               list[k] = list[k + 1];
            }
            length--;
         } else
            j++;
      }
   }

}
Sign up to request clarification or add additional context in comments.

Comments

0

What you are doing is subtracting every int in your list by 1. This will not remove the duplicates. To remove an element from an array, check out: How to remove an element from an array in C?

Comments

0

The function does not make a sense. For example the condition in this if statement

if (list[i] == list[i])
    duplicate = list[i];

is always true because each object is equal to itself.

And the function does not modify the array.

You can not resize the array but you can gather all unique elements in the beginning of the array and return the number of the unique elements.

Here is a demonstrative program.

#include <stdio.h>

size_t generateUniqeList( int *a, size_t n )
{
    size_t m = 0;

    for ( size_t i = 0; i < n; i++ )
    {
        size_t j = 0;
        while ( j < m && a[j] != a[i] ) j++;

        if ( j == m )
        {
            if ( m != i ) a[m] = a[i];
            ++m;
        }
    }

    return m;
}   

int main(void) 
{
    int list[] = { 5, 5, 4, 3, 2, 1 };
    const size_t N = sizeof( list ) / sizeof( *list );

    for ( size_t i = 0; i < N; i++ ) printf( "%d ", list[i] );
    putchar( '\n' );

    size_t m = generateUniqeList( list, N );

    for ( size_t i = 0; i < m; i++ ) printf( "%d ", list[i] );
    putchar( '\n' );

    return 0;
}

Its output is

5 5 4 3 2 1 
5 4 3 2 1 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.