-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path08_print_unique.c
175 lines (144 loc) · 4.09 KB
/
08_print_unique.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// // Write a function in C to print all unique elements in an array.
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
// // Functions Declarations (Prototypes)
void printArray(int[], int);
void inputArray(int[], int);
void sortAsc(int[], int);
int linearSearch(int[], int, int);
void copyArray(int[], int, int[]);
void printUniqueElements(int[], int);
// // Main Function Start
int main()
{
const int ARRAY_SIZE;
printf("\nHow Many Elements You Want to Enter => ");
scanf("%d", &ARRAY_SIZE);
// // Check for Invalid Array Size
if (ARRAY_SIZE < 1)
{
puts("\n!!! Invalid Input, Plz Correctly Specify Number of Elements...");
exit(0);
}
// // Declare Array of Variable size
int arr[ARRAY_SIZE];
// // Input Elements
printf("\nEnter %d Elements => ", ARRAY_SIZE);
inputArray(arr, ARRAY_SIZE);
// // Print Array
puts("\n\n>>>>>>>> Entered Elements <<<<<<<<<");
printArray(arr, ARRAY_SIZE);
puts("\n\n>>>>>>>> Unique Elements in Array <<<<<<<<<");
printUniqueElements(arr, ARRAY_SIZE);
putch('\n');
getch();
return 0;
}
// // Main Function End
// // Functions Definitions 👇👇
// // Function to Display Array Elements
void printArray(int arr[], int size)
{
putch('\n'); // // Add new line
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
putch('\n'); // // Add new line
}
// // Function to Input Array Elements
void inputArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);
}
// // Function to Sort an Array in Ascending Order
void sortAsc(int arr[], int size)
{
// // Bubble Sort
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (arr[j] < arr[j + 1]) // // true, then swap
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// // Function to Search An Element Using Linear Search
int linearSearch(int arr[], int size, int search)
{
for (int i = 0; i < size; i++)
{
if (arr[i] == search)
return i;
}
return -1;
}
// // Function to Copy Array
void copyArray(int arr[], int size, int arr2[])
{
for (int i = 0; i < size; i++)
arr2[i] = arr[i];
}
// // Function to Print Unique Elements in Array
void printUniqueElements(int arr[], int size)
{
putch('\n');
// // 1st Approach (using sorting)
// // int countDup = 0, copyOfArr[size], foundUnique = 0;
// // copyArray(arr, size, copyOfArr); // // copy arr into copyOfArr
// // sortAsc(copyOfArr, size); // // sort array
// // if (size > 1 && copyOfArr[0] != copyOfArr[1])
// // {
// // printf("%d ", copyOfArr[0]);
// // foundUnique = 1;
// // }
// // else if (size == 1)
// // {
// // printf("%d ", copyOfArr[0]);
// // foundUnique = 1;
// // }
// // for (int i = 1; i < size - 1; i++)
// // {
// // if (copyOfArr[i] != copyOfArr[i - 1] && copyOfArr[i] != copyOfArr[i + 1])
// // {
// // printf("%d ", copyOfArr[i]);
// // foundUnique = 1;
// // }
// // }
// // if (size > 1 && copyOfArr[size - 1] != copyOfArr[size - 2])
// // {
// // printf("%d ", copyOfArr[size - 1]);
// // foundUnique = 1;
// // }
// // if (!foundUnique)
// // puts("\nThere Are No Unique Elements In Array...");
// // 2nd Approach
int inspected[size], k = 0, countDup = 0, foundUnique = 0;
for (int i = 0; i < size; i++)
{
if (linearSearch(inspected, k, arr[i]) == -1)
{
for (int j = 0; j < size; j++)
{
if (arr[i] == arr[j] && i != j)
countDup++;
}
if (countDup == 0)
{
printf("%d ", arr[i]);
foundUnique = 1;
}
inspected[k++] = arr[i];
countDup = 0;
}
}
if (!foundUnique)
puts("\nThere Are No Unique Elements In Array...");
putch('\n');
}