Write function for removing duplicate elements from a given array and sort that array. (Without using inbuilt functions). For Example: Array = {2, 1, 3, 2, 4, 4, 6, 5, 6} Output = {1, 2, 3, 4, 5, 6}
1
Expert's answer
2013-06-10T08:48:36-0400
#include <iostream> using namespace std;
int main() { cout << "Input array length: "; int n; cin >> n; int a[100]; for (int i = 0; i < n; i++) { & cout << "Input element " << i << ": "; & cin >> a[i]; } for (int i = 0; i < n; i++) & for (int j = 0; j < n-1; j++) if (a[j] > a[j+1]) swap(a[j], a[j+1]); for (int i = 1; i < n; i++) { & if (a[i] == a[i-1]) { for (int j = i; j < n-1; j++) a[j] = a[j+1]; --n; --i; & } }
cout << "Resulting array: " << endl; for (int i = 0; i < n; i++) & cout << a[i] << " "; cout << endl; }
Comments
Leave a comment