write a function to remove duplicate elements from an array and return the array with unique integer values only in c
Answers
Just remove the output (printf) and you have an array with non duplicate values at the end. The sorting of the result array is so that the first values of duplicates is removed and the last is kept. You could change the direction of compare from last to first instead to get another sorting.
#include <stdio.h>
/* Initial array of integers */
const int INTARRAY[] = {12,45,46,78,2,45,33,72,12,56,77,54,45,99,345,33};
/* Index used during cleaning to
count the number of cleaned integers */
int cleanIndex = 0;
/* The number of integers in the
the initial array is
its total length divided by
the length of an integer. */
int lengthOfArray = sizeof(INTARRAY)/sizeof(int);
/* Create a new array to hold the non
duplicated integers during the cleaning
process. Since we do not now its final
size we just make it same length as the
initial array */
int cleanArray[sizeof(INTARRAY)/sizeof(int)];
void findDuplicate(int index) {
/* Get the integer to be tested */
int intToBeTested = INTARRAY[index];
/* If the index gets equal to or larger
than the length of the initial array,
then the process is finished and we
can return to main() */
if (index >= lengthOfArray) {return;}
else {
/* The index of the test should start
at offset +1 from the testsubject */
int tempIndex = index+1;
/* As long as the test is negative
and we have not yet reach the end,
increase the index to test against
the next number */
while (intToBeTested != INTARRAY[tempIndex] && tempIndex < lengthOfArray) {
tempIndex++;
}
/* If the test reached the end without any match
there was no duplicate and we can store this value. */
if (tempIndex == lengthOfArray) {
/* Store this value in the cleaned array */
cleanArray[cleanIndex] = INTARRAY[index];
cleanIndex++;
}
/* Increase index and recurse */
index++;
findDuplicate(index);
}
}
int main()
{
printf("\nThere are %i numbers in the array before cleaning\n", lengthOfArray);
for (int i = 0; i < lengthOfArray; i++) {
printf("%i ",INTARRAY[i]);
}
/* Call the recursive function with
the initial index of the first occurence. */
findDuplicate(0);
printf("\n\nThere are %i numbers in the array after cleaning\n", cleanIndex);
/* There is no way to change the length of the
initial arrays, so we have to create
// a new array and move the values from
the cleaned array to the new array */
int resultArray[cleanIndex];
for (int i; i < cleanIndex; i++) {
resultArray[i] = cleanArray[i];
printf("%i ",resultArray[i]);
}
printf("\n");
return 0;
}
Function to remove duplicate elements from an array:
Explanation:
- Just remove the output (printf) and you have an array with non duplicate values at the end.
- #include <stdio.h>
- /* Initial array of integers */
- const int INTARRAY = {12,45,46,78,2,45,33,72,12,56,77,54,45,99,345,33};
- /* Index used during cleaning to
- count the number of cleaned integers */
- int cleanIndex = 0;
- /* The number of integers in the
- the initial array is
- its total l