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
parent
b2cf773646
commit
ca93cfa934
|
@ -7,6 +7,7 @@
|
||||||
3. [Left Rotation of Array](c-or-cpp/left-rotation.cpp)
|
3. [Left Rotation of Array](c-or-cpp/left-rotation.cpp)
|
||||||
4. [Shift Negatives in Array](c-or-cpp/shift-negatives.cpp)
|
4. [Shift Negatives in Array](c-or-cpp/shift-negatives.cpp)
|
||||||
5. [Maximum Subarray Sum](c-or-cpp/max-subarray-sum.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
|
### Python
|
||||||
|
|
||||||
|
|
|
@ -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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue