How it works...

We will define a macro called max of size 100. Two arrays, p and q, are defined of size max. Array p will contain the original elements, and array q will contain the unique elements of array p. You will be prompted to enter the length of the array and, thereafter, using the for loop, the elements of the array will be accepted and assigned to array p.

The following statement will assign the first element of array p to the first index location of our blank array, which we will name array q:

q[0]=p[0]

A for loop is again used to access the rest of the elements of array p, one by one. First, the foremost element of array p, which is at p[0], is copied to array q at q[0].

Next, the second array p element, p[1], is compared with all the existing elements of array q. That is, p[1] is checked against array q to confirm whether it is already present there.

Because there is only a single element in array q,  p[1] is compared with q[0]. Because p[1] does not exist in array q, it is copied at q[1].

This procedure is repeated for all elements in array p. Each of the accessed elements of array p is run through the ifexists() function to check whether any of them already exist in array q

The function returns 1 if an element in array p already exists in array q. In that case, the element in array p is discarded and the next array element is picked up for comparison.

In case the ifexists() function returns 0, confirming that the element in array p does not exist in array q, the array p element is added to array q at the next available index/subscript location.

When all the elements of array p are checked and compared, array q will have only the unique elements of array p.

Let's use GCC to compile the uniqueelements.c program as follows:

D:\CBook>gcc uniqueelements.c -o uniqueelements

Now, let's run the generated executable file, uniqueelements.exe, to see the output of the program:

D:\CBook>./uniqueelements
Enter the length of the array:5
Enter 5 elements in the array
1
2
3
2
1

The unique elements in the array are:
1
2
3

Voilà! We've successfully identified the unique elements in an array. Now, let's move on to the next recipe!