From ca93cfa934a688c88187acfb67c1fc019839d83d Mon Sep 17 00:00:00 2001 From: Sanjay PS <53363573+Dabohemianstarlord@users.noreply.github.com> Date: Wed, 14 Apr 2021 18:52:22 +0530 Subject: [PATCH] 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> --- arrays/README.md | 1 + arrays/c-or-cpp/unique-elements-in-an-array.c | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 arrays/c-or-cpp/unique-elements-in-an-array.c diff --git a/arrays/README.md b/arrays/README.md index 36adc682..28138a3f 100644 --- a/arrays/README.md +++ b/arrays/README.md @@ -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 diff --git a/arrays/c-or-cpp/unique-elements-in-an-array.c b/arrays/c-or-cpp/unique-elements-in-an-array.c new file mode 100644 index 00000000..4eea61ef --- /dev/null +++ b/arrays/c-or-cpp/unique-elements-in-an-array.c @@ -0,0 +1,42 @@ +#include +#include + +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