Knowee
Questions
Features
Study Tools

c program to print unique elements in an array

Question

c program to print unique elements in an array

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C program that prints the unique elements in an array:

#include <stdio.h>

int main() {
    int arr[100], i, j, k, size;

    printf("Enter size of the array : ");
    scanf("%d", &size);

    printf("Enter elements in array : ");
    for(i=0; i<size; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Unique elements in array are : ");
    for(i=0; i<size; i++) {
        for(j=0; j<i; j++){
           if(arr[i] == arr[j])
               break;
        }
        
        if(i == j)
            printf("%d ", arr[i]);
    }

    return 0;
}

Here's how it works:

  1. The program first asks the user to enter the size of the array.
  2. Then it asks the user to enter the elements of the array.
  3. It then starts a loop to go through each element of the array.
  4. For each element, it starts another loop that checks all the previous elements for a match.
  5. If it finds a match, it breaks out of the loop.
  6. If it doesn't find a match (which means the element is unique), it prints the element.

This program assumes that the array will have less than 100 elements. If you want to work with larger arrays, you can increase the size of the arr array.

This problem has been solved

Similar Questions

write a c program to print unique prime numbers in array

Create an array of integers, then create an array of pointers pointing to each element in the integer array. Print the values using both arrays. Write the code in c

roblem statementGinny, an inquisitive mind fascinated by the intricacies of arrays, has posed a challenge for you. She needs a program that can identify and display the unique elements present in an array. Write a program that includes a function named findUniqueElements where the array is passed as an argument.Input format :The first line of input is an integer value 'N', representing the size of the array.The second line of input consists of N space-separated integers arr[i], representing the elements of the array.Output format :The output displays the integers, representing the unique elements identified within the array, separated by spaces.

Write a function, which prints all numbers in common between two given arrays, with each number printed only once.  The function returns number of common elements.Assume that both arrays contain each number only once.int common(int *a, int lena, int *b, int lenb);Note: the function does printing.Note: in the output shown, the last number printed is the return value of the function common.

Below is an array declaration in 'C' language:int ar1[] = {2,9};int ar2[3] = {9};What will be the output of the following print statement?printf("%d,%d", ar1[1], ar2[2]);(1 Point)

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.