Added program in c for printing unique elements in an array (#143)

* Added program in c for printing unique elements in an array

* Update unique-elements-in-an-array.c

* Update unique-elements-in-an-array.c

* Update unique-elements-in-an-array.c

* Update unique-elements-in-an-array.c

- Make array declaration dynamic
- Formatted the output
- Cleanup of extra spaces in the program

Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com>
pull/185/head
Sanjay PS 2021-04-14 18:52:22 +05:30 committed by GitHub
parent b2cf773646
commit ca93cfa934
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

View File

@ -7,6 +7,7 @@
3. [Left Rotation of Array](c-or-cpp/left-rotation.cpp)
4. [Shift Negatives in Array](c-or-cpp/shift-negatives.cpp)
5. [Maximum Subarray Sum](c-or-cpp/max-subarray-sum.cpp)
6. [Unique Elements in an Array](c-or-cpp/unique-elements-in-an-array.c)
### Python

View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
int main(){
int size;
/* take the size of the array from user as input */
printf("Enter size of the array: ");
scanf("%d", &size);
/* dynamically create an array of the reqruired size */
int *a = (int*)malloc(sizeof(int)*size);
if(size<0){
size = size*(-1);
}
/* enter the elements to the array */
printf("Enter elements of the array: ");
for(int k=0; k<size; k++){
scanf("%d", &a[k]);
}
int flag;
printf("Unique elements of the array are : ");
for(int i=0; i<size; i++){
flag = 0;
for(int j=0; j<=size; j++){
if(i != j && a[i] == a[j]){
flag++;
}
}
if(flag == 0){
printf("%d ", a[i]);
}
}
}