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;
}
Cutik
  • 55
  • 4

3 Answers3

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++;
      }
   }

}
Adam
  • 856
  • 2
  • 9
  • 18
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?

Community
  • 1
  • 1
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 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335