0% found this document useful (0 votes)
25 views

Uniqe Element in Array

This document contains a coding test with 1 question to write a C program that prints all unique elements in an array. The program defines an array arr to store input elements, another array freq to store the frequency of each element, and variables to get the array size and iterate through the arrays. It checks each element's frequency by comparing it to other elements, stores the count in freq, and finally prints any elements with a freq of 1, indicating unique elements.

Uploaded by

Pranay Mudhiraj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Uniqe Element in Array

This document contains a coding test with 1 question to write a C program that prints all unique elements in an array. The program defines an array arr to store input elements, another array freq to store the frequency of each element, and variables to get the array size and iterate through the arrays. It checks each element's frequency by comparing it to other elements, stores the count in freq, and finally prints any elements with a freq of 1, indicating unique elements.

Uploaded by

Pranay Mudhiraj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

FACE TCS Ninja - Coding_3

TEST CODE : TCS Ninja -


Coding_3
Total number of question : 1
Test duration (min) : 20 min
Correct attempt (mark) : NA
Wrong attempt (mark) : NA

CODING

Program to print all the unique elements in the array.


#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE], freq[MAX_SIZE];
int size, i, j, count;
scanf("%d", &size);
// Get the N elements as input
// Assign the array freq[] as -1 (i.e., as default value)
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
freq[i] = -1;
}
// Check the frequency of all the elements in an array
for(i=0; i<size; i++)
{
count = 1;
for(j=i+1; j<size; j++)
{
if(arr[i] == arr[j])
{
count++;
freq[j] = 0;
}
}
// Store the number of occurrences of each element of the given array
if(freq[i] != 0)
{
freq[i] = count;
}
}
// Print only the unique elements
// If freq[i] value is 1, then it has occured only one time
for(i=0; i<size; i++)
{
if(freq[i] == 1)
{
printf("%d ", arr[i]);
}
}
return 0;
}
______________________________________________________________________________________________________
Focus Academy for Career Enhancement Page 1 of 1

You might also like