/* Include lib */
#include <stdio.h>
#include <assert.h>

/* Define */
#define ARRAY_MAX_NUM     8
#define ARRAY_INPUT       { 0, 1, 2, 3, 4, 3, 6, 7 }
#define ARRAY_INPUT1      { 1, 1, 1, 1, 1, 1, 1, 1 }

int array_input[ARRAY_MAX_NUM] = ARRAY_INPUT1;
int array_num = ARRAY_MAX_NUM;

/* API delete a element in array */
int del_element_in_array(int *arr, int *arr_size, int index)
{
  int i, size;

  if (!arr || !arr_size || (*arr_size == 0)) {
    printf("WARN: ARRAY empty\n");
    return -1;
  }

  if (index > *arr_size) {
    printf("ERR: Index is over array size\n");
    return -1;
  }

  size = *arr_size;
  for (i = index; i < (size - 1); i++)
    arr[i] = arr[i + 1];

  /* Clear last element after shifting */
  arr[size - 1] = 0;

  /* And reduce array size */
  *arr_size = size - 1;

  return 0;
}

/* API delete repeated element in array */
int del_repeat_in_array(int *arr, int *arr_size)
{
  int i, j, size;

  if (!arr || !arr_size || (*arr_size == 0)) {
    printf("WARN: ARRAY empty\n");
    return -1;
  }

  size = *arr_size;
  for (i = 0; i < size; i++) {
    for (j = 0; j < size; j++) {
      /*
       * If find matching element, delete it.
       * Then continue with this index again
       * because of shifting in delete.
       */
      if ((arr[i] == arr[j]) && (i != j)) {
        if (del_element_in_array(arr, &size, j))
          return -1;
        j--;
      }
    }
  }
  
  /* Update array size */
  *arr_size = size;
  return 0;
}

/* Main func interract with user */
int main(void)
{
  int i;

  printf("Input  array: ");
  for (i = 0; i < array_num; i++)
    printf("%d ", array_input[i]);
  printf("\n");

  /* Delete repeated in array */
  if (del_repeat_in_array(&array_input[0], &array_num))
    return -1;

  printf("Output array: ");
  for (i = 0; i < array_num; i++)
    printf("%d ", array_input[i]);
  printf("\n");

  return 0;
}



#if 0 /* delete_repeat_bad_solution_array_order_change_tg */
/* Include lib */
#include <stdio.h>
#include <assert.h>

/* Define */
#define ARRAY_MAX_NUM   16
#define ARRAY_INPUT     { 1,  2,  3,  4,  5,  6,  7,  8, \
                         -1, -2,  3, -4, -5, -6, -7, -8 }

#define ARRAY_INPUT1    { 1,  1,  1,  1,  1,  1,  1,  1, \
                          1,  1,  1,  1,  1,  1,  1,  1 }

int array_input[ARRAY_MAX_NUM] = ARRAY_INPUT;
int array_size = ARRAY_MAX_NUM;

/* API Delete repeated element in array */
int delete_repeat_in_array(int *arr, int *arr_size)
{
  int i, j, tmp, size;

  if (!arr || !arr_size || (*arr_size == 0)) {
    printf("WARN: Array empty\n");
    return -1;
  }

  /* Start with each element */
  size = *arr_size;
  for (i = 0; i < size; i++) {
    /* Compare this element with all array */
    for (j = 0; j < size; j++) {
      /*
       * If find any matching and not itself, swap it with last element.
       * Remove last element (means repeater) by reducing array size 1
       */
      if ((arr[i] == arr[j]) && (i != j)) {
        tmp = arr[j];
        arr[j] = arr[size - 1];
        arr[size - 1] = tmp;
        size--;
        j--;
      }
    }
  }

  *arr_size = size;
  return 0;
}

/* Main func interract with user */
int main(void)
{
  int i;

  printf("Input  Array (size %2d): ", array_size);
  for (i = 0; i < array_size; i++)
    printf("%2d ", array_input[i]);
  printf("\n");

  /* Delete repeat */
  if (delete_repeat_in_array(&array_input[0], &array_size))
    return -1;

  printf("Output Array (size %2d): ", array_size);
  for (i = 0; i < array_size; i++)
    printf("%2d ", array_input[i]);
  printf("\n");  

  return 0;
}
#endif

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: