Latest :

C Program : Non-Repeating Elements of An Array | C Programs

The problem statement given is, to write a C program to display the non-repeating elements in a given array. To find the non-repeating characters, we require the number of elements in the array and the data values or elements of the array as inputs.

Our desired output at the end is for all the non-repeating elements in this array to be displayed on the screen.

To gather all the inputs at runtime, we can make use of predefined input function, scanf(). This function reads the input of any primitive datatype like int, char, double, float, etc., at runtime from the keyboard.

Based on the format specifier mentioned in the function, the datatype of the input being read is determined. Since all our inputs in the given problem statement are integers, we’ll be making use of ‘%d’ format specifier.

Firstly, we’ll read the number of elements or size of the array (n) and create an array (a) of the corresponding size. Then, we iterate from beginning to end and read the element or data value at each index position (a[i]).

printf(“\nEnter the number of elements in an array: “);

scanf(“%d”,&n);

int a[n];

printf(“\nEnter %d elements in an array :\n”,n);

for(i = 0; i < n; i++) {

scanf(“%d”,&a[i]);

}

After getting all the necessary inputs, we’ll make a function call to a user-defined function (nonRepeatingint) and pass this input array and size as parameters. This function has the logic to display the non-repeating elements of array.

For this, we’ll require a count variable which is initially zero, for checking whether there is non-repeating elements of the array. Then, we iterate in a loop from beginning (i=0) to end (n) and check for every element, whether it is repeating or not.

We’ll maintain another variable c which is used to check if the given element is repeating or not so, its initialized to zero for every outer iteration.

So, for every element in the array, we iterate through the loop (j=0 to n) and check if there is a same element existing in an index position apart from itself i.e., if a[i]=a[j] where i!=j (i not equals to j).

If yes then, we increment the c variable by 1 and break the inner loop. After the iteration of inner loop, in the outer loop we check the value of c, if c is equal to 0 it means that, it is a non-repeating element hence, we display this element (a[i]) on the console screen using predefined output function, printf() and the count variable is made 1.

int c,i,j,count=0;

for(i = 0; i < n; i++) {

c=0;

for(j = 0; j < n; j++) {

if(a[i]==a[j] && i!=j) {

c++;

break;

}

}

if(c==0) {

printf(” %d”,a[i]);

count=1;

}

}

This continues the checking of all the elements, and if they are non-repeating then, they are displayed on the console screen. This function returns the count variable to the main method. In the main method, we check if the count value is equal to 0. If yes then it means, there are no non-repeating elements in the array so we display the same message on the console screen.

count=nonRepeatingint(a,n);

if(count==0)

printf(“\nThere are no Non repeating elements in an array “);

Non Repeating Elements in An Array C Program

Output:

Type – 2

Earlier we have seen the logic behind displaying all the non-repeating elements in the given array by using functions. The advantage functions have is that, they made the code reusable and increase the readability of the code.

If same logic is needed elsewhere, we can just make a function call and pass required parameters instead of, rewriting the same set of statements. Also, as they are split based on their functionality and placed within separate function blocks, it is cleaner and more readable.

But, even if the entire code is within main method or within function, the time and space complexity still remain the same because, the logic used and the set of statements are still the same. So, if one is sure that, they do not have to reuse the logic then, placing it within main method causes no harm. Below we can see the entire code within main method using the same logic discussed above.

x

Check Also

C Program To Print Number Of Days In A Month | Java Tutoring

C program to input the month number and print the number of days in that ...