write a c program to print non repeted numbers of an given array.
How to Print Non-Repeated Numbers in an Array using C
"Write a C program to print non-repeated numbers of a given array"
means that you need to write a program in the C programming language that takes an array of numbers as input and then prints only the numbers that appear once in the array, without repeating any number.
In other words, you need to write a program that reads the array elements and checks which elements are unique (i.e., appear only once in the array). Then, you should output only those unique elements of the array.
For example, if the given array is [1, 2, 2, 3, 4, 4, 5],
the non-repeated numbers are [1, 3, 5].
Therefore, the program should output [1, 3, 5].
Note that in the example array, 2 and 4 appear more than once, so they are not considered as non-repeated numbers and should not be printed by the program.
C program that prints the non-repeated numbers of a given array
C programming
#include
int main() {
int arr[10]; // array to store 10 numbers
int freq[10] = {0}; // array to store frequency of each number
int i, j, count = 0;
// Input 10 numbers into array
printf("Enter 10 numbers: ");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// Count the frequency of each number
for (i = 0; i < 10; i++) {
for (j = i + 1; j < 10; j++) {
if (arr[i] == arr[j]) {
freq[i]++;
freq[j]++;
}
}
}
// Print non-repeated numbers
printf("Non-repeated numbers: ");
for (i = 0; i < 10; i++) {
if (freq[i] == 0) {
printf("%d ", arr[i]);
count++;
}
}
if (count == 0) {
printf("No non-repeated numbers found\n");
}
return 0;
}
Output of the given C code is:
Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10
Non-repeated numbers: 1 2 3 4 5 6 7 8 9 10
This is because the code first inputs 10 numbers into an array called arr. Then, it creates another array called freq to store the frequency of each number in the arr array. The code then iterates over the arr array and compares each element to the other elements in the array. If two elements are equal, the code increments the frequency of both elements in the freq array.
Further Reading:
write a c program to reverse the elements of a given array.
write a c program to delete an element at desired from an array
write a c program to insert an element at a desired position in an array
write a c program that deletes the duplicate elements of an array.
write a c program to find the duplicate elements of a given array and find the count of duplicate elements.
write a c program to print non-repeated numbers of a given array.
Enroll Now:
[ C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"
Contact Us:
- For any inquiries, please email us at [[email protected]].
- Follow us on insta [ electro4u_offical_ ] for updates and tips.
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!