I am having trouble achieving the wanted results. The program should ask for 20 inputs and then go over each to see if they appear more than once. Then only print out those that appeared once.
However currently my program prints out random numbers that are not inputted.
For example:
array = {10,10,11,12,10,10,10.....,10} should return 11 and 12
#include <stdio.h>
void main() {
int count, size=20, array[size], newArr[size];
int number=0;
for(count = 0; count < size; count++) {
// Ask user for input until 20 correct inputs.
printf("\nAnna %d. luku > ", count+1);
scanf("%d", &number);
if( (number > 100) || (number < 10) ) {
while(1) {
number = 0;
printf("Ei kelpaa.\n");//"Is not valid"
printf("Yrita uudelleen > ");//"Try again >"
scanf("%d", &number);
if ( (number <= 100) && (number >= 10) ) {
break;
}
}
}
array[count] = number;
}
for(int i=0; i < size; i++) {
for(int j=0; j<size; j++){
if(array[i] == array[j]){
size--;
break;
} else {
// if not duplicate add to the new array
newArr[i] == array[j];
}
}
}
// print out all the elements of the new array
for(int k=0; k<size; k++) {
printf("%d\n", newArr[k]);
}
}
You don't need the newArr here, or the separate output loop. Only keep a count that you reset to zero at the beginning of the outer loop, and increase in the inner loop if you find a duplicate.
Once the inner loop is finished, and the counter is 1 then you don't have any duplicates and you print the value.
In code perhaps something like:
for (unsigned i = 0; i < size; ++i)
{
unsigned counter = 0;
for (unsigned j = 0; j < size; ++j)
{
if (array[i] == array[j])
{
++counter;
}
}
if (counter == 1)
{
printf("%d\n", array[i]);
}
}
Note that the above is a pretty naive and brute-force way to deal with it, and that it will not perform very well for larger array sizes.
Then one could implement a hash-table, where the value is the key, and the count is the data.
Each time you read a value you increase the data for that value.
Once done iterate over the map and print all values whose data (counter) is 1.
Use functions!!
Use proper types for indexes (size_t).
void printdistinct(const int *arr, size_t size)
{
int dist;
for(size_t s = 0; s < size; s++)
{
int val = arr[s];
dist = 1;
for(size_t n = 0; n < size; n++)
{
if(s != n)
if(val == arr[n]) {dist = 0; break;}
}
if(dist) printf("%d ", val);
}
printf("\n");
}
int main(void)
{
int test[] = {10,10,11,12,10,10,10,10};
printdistinct(test, sizeof(test)/sizeof(test[0]));
fflush(stdout);
}
https://godbolt.org/z/5bKfdn9Wv
This is how I did it and it should work for your:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
void printdistinct(const int *muis, size_t size);
int main()
{
int loop=20,i,muis[20],monesko=0;
for(i=0; i<loop; i++){
monesko++;
printf ("Anna %d. luku: \n",monesko);
scanf("%d", &muis[i]);
if (muis[i]<10 || muis[i]>100){
printf("Ei kelpaa!\n");
muis[i] = muis[i + 1];
printf("YRITÄ UUDELLEEN:\n ");
scanf("%d", &muis[i]);
}
}
printdistinct(muis, sizeof(muis)/sizeof(muis[0]));
fflush(stdout);
return 0;
}
void printdistinct(const int *muis, size_t size)
{
for(size_t s = 0; s < size; s++)
{
int a = muis[s];
int testi = 1;
for(size_t n = 0; n < size; n++){
if(s != n) {
if(a == muis[n]){
testi = 0;
break;
}
}
}
if(testi) {
printf("%d \n", a);
}
testi = 1;
}
printf("\n");
}
This approach uses some memory to keep track of which elements are duplicates. The memory cost is higher, but the processor time cost is lower. These differences will become significant at higher values of size.
char* duplicate = calloc(size, 1); // values are initialized to zero
for (unsigned i = 0; i < size; ++i)
{
if(!duplicate[i]) // skip any value that's known to be a duplicate
{
for (unsigned j = i + 1; j < size; ++j) // only look at following values
{
if (array[i] == array[j])
{
duplicate[i] = 1;
duplicate[j] = 1; // all duplicates will be marked
}
}
if (!duplicate[i])
{
printf("%d\n", array[i]);
}
}
}
What you can do is you can initialize a hashmap that will help you store the unique elements. Once you start iterating the array you check for that element in the hashmap. If it is not present in the hashmap add it to the hashmap. If it is already present keep iterating.
This way you would not have to iterate the loop twice. Your time complexity of the algorithm will be O(n).
unordered_map < int, int > map;
for (int i = 0; i < size; i++) {
// Check if present in the hashmap
if (map.find(arr[i]) == map.end()) {
// Insert the element in the hash map
map[arr[i]] = arr[i];
cout << arr[i] << " ";
}
}
Related
I am working on a school assignment and currently I'm a stuck on a part or it.
The piece of the program that I am showing further down, is supposed to take 7 numbers, check if they are above 0 and below maxValue, then, another function, checkIfContains should check if there are any duplicates in that array and return true or false to the first function, enterRow.
If everything is fine (numbers are within range and no duplicates), then nothing more is to be done by the functions here.. ( I have isolated these two. There are more where these came from, we are supposed to make a lottery game)
What am I doing wrong?
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
void enterRow(int numbers[], int len, int maxValue);
bool checkIfContains(int digit, int arrayToCheck[], int len);
int main(void)
{
int len = 7;
int maxValue = 39;
int numbers[len];
memset(numbers, 0, sizeof(int) * len);
enterRow(numbers, len, maxValue);
return 0;
}
void enterRow(int numbers[], int len, int maxValue)
{
int flag = 1;
printf("\nEnter your lotto row (%d values between 1-%d): ", len, maxValue);
do
{
for(int i = 0; i < len; i++) scanf(" %d", &numbers[i]);
for (int i = 0; i < len; i++)
{
if (numbers[i] < 1 || numbers[i] > maxValue)
{
printf("Numbers must be between 1-%d, try again!\n", maxValue);
break;
}
if (checkIfContains(numbers[i], numbers, len) == true)
{
printf("Duplicate, try again: ");
break;
}
else
{
flag = 2;
}
}
} while (flag == 1);
}
bool checkIfContains(int digit, int arrayToCheck[], int len)
{
for (int i = 1; i < len; i++)
{
if (arrayToCheck[i] == digit)
{
return true;
}
else
{
return false;
}
}
}
A few issues:
The check function will check the current value against itself, so the array will always have a duplicate
You only want to return false at the end of the loop
There is no return at the end of the function
Your loop was starting with 1 but needs to start with 0
To fix, rather than pass a match value, pass the index of the value.
Here's the refactored code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
void enterRow(int numbers[], int len, int maxValue);
bool checkIfContains(int curidx, int arrayToCheck[], int len);
int
main(void)
{
int len = 7;
int maxValue = 39;
int numbers[len];
memset(numbers, 0, sizeof(int) * len);
enterRow(numbers, len, maxValue);
return 0;
}
void
enterRow(int numbers[], int len, int maxValue)
{
int flag = 1;
printf("\nEnter your lotto row (%d values between 1-%d): ", len, maxValue);
do {
for (int i = 0; i < len; i++)
scanf(" %d", &numbers[i]);
for (int i = 0; i < len; i++) {
if (numbers[i] < 1 || numbers[i] > maxValue) {
printf("Numbers must be between 1-%d, try again!\n", maxValue);
break;
}
if (checkIfContains(i, numbers, len) == true) {
printf("Duplicate, try again: ");
break;
}
else {
flag = 2;
}
}
} while (flag == 1);
}
bool
checkIfContains(int curidx, int arrayToCheck[], int len)
{
int digit = arrayToCheck[curidx];
for (int i = 0; i < len; i++) {
// don't check number against itself
if (i == curidx)
continue;
if (arrayToCheck[i] == digit)
return true;
}
return false;
}
The function checkIfContains has two defects.
First of all the loop shall start from the next position of the element that is passed to the function as a value of the parameter digit. It means that the function should be called at least like like
if (checkIfContains(numbers[i], numbers + i + 1, len - i - 1 ) == true)
And within the function the for loop shall start from 0
for (int i = 0; i < len; i++)
instead of 1 as in your function
for (int i = 1; i < len; i++)
The second defect is that you shall not return false as soon as unequal element is found in the array.
Using your approach The function can look the following way
bool checkIfContains(int digit, const int arrayToCheck[], int len)
{
int i = 0;
while ( i < len && arrayToCheck[i] != digit ) ++i;
return i != len;
}
And as I already mentioned the function must be called like
if (checkIfContains(numbers[i], numbers + i + 1, len - i - 1 ) == true)
Though your approach too complicated. You could check entered values in the first for loop
for(int i = 0; i < len; i++)
{
scanf(" %d", &numbers[i]);
// 1) check whether the value is in the range
// 2) check whether the value is unique
//...
}
To check if there are duplicates in an array, the most basic way is to loop through the array using two nested for loops and compare each element with the others, like so:
int array[5] = {5, 7, 6, 3, 5};
int arrayLength = 5;
for(int i = 0; i < arrayLength; i++)
{
for(int j = i + 1; j < arrayLength; j++)
{
if(array[i] == array[j])
{
printf("%d has duplicate\n", array[i]);
}
}
}
You can make your code like:
bool hasDuplicate(int array[], int arrayLength)
{
for(int i = 0; i < arrayLength; i++)
{
for(int j = i + 1; j < arrayLength; j++)
{
if(array[i] == array[j])
{
return true;
}
}
}
return false;
}
And you can make enterRow like this:
void enterRow(int numbers[], int len, int maxValue)
{
printf("\nEnter your lotto row (%d values between 1-%d): ", len, maxValue);
for(int i = 0; i < len; i++)
scanf(" %d", &numbers[i]);
for(int i = 0; i < len; i++)
{
if(numbers[i] > maxValue || numbers[i] < 1)
{
printf("Format is not correct.\n");
while(1);
}
}
if(hasDuplicate(numbers, len) == true)
{
printf("The numbers cannot be duplicates.\n");
while(1);
}
}
This way your code is much cleaner and more readable. Obviously, you should optimize this code to how you want to deal with the error cases and etc.
this is the program I made ,if I input [13,11,10,17,18] i get the output [12,13,17,11,10]. I do not understand what mistake I am making. somebody please help me understand.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n,j,i,num,v;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Entered number of elements: %d\n", n);
ptr = (int*)malloc(n * sizeof(int));
for (i = 0; i < n; ++i) {
scanf("%d",&v);
ptr[i] = v;
}
i=0;
j=0;
while(i<5){
j++;
if (ptr[j]%2==0 && i%2==0){
num=ptr[i];
ptr[i]=ptr[j];
ptr[j]=num;
}
if (ptr[j]%2!=0 && i%2 !=0){
num=ptr[i];
ptr[i]=ptr[j];
ptr[j]=num;
}
if (j==4){
i++;
j=0;
}
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
Ok, I tried to tell you how you should have written you program, but you didn't listen:
Make a https://stackoverflow.com/help/minimal-reproducible-example
A MCVE needs all the includes
No interactive stuff. You need to run and run and run your program in a debugger. You don't want to put data in manually every single time.
You want many tests, and you want to repeat them, so that when you fix one, you don't break another.
Make a function which does the job.
Free your memory!
Now to the solution: your idea of a solution was fine apart from the stuff about indexes. It's pretty similar to the one you will find down here. The only difference is that I put the odd numbers al the end to avoid checking elements multiple times.
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
void evenodd(int *v, size_t n)
{
for (size_t i = 0; i < n; ++i) {
while (i < n && v[i] % 2 == 0) {
++i;
}
--n;
while (i < n && v[n] % 2) {
--n;
}
if (i < n) {
int tmp = v[i];
v[i] = v[n];
v[n] = tmp;
}
}
}
bool is_evenodd(int *v, size_t n)
{
size_t i = 0;
while (i < n && v[i] % 2 == 0) {
++i;
}
while (i < n && v[i] % 2 != 0) {
++i;
}
return i == n;
}
void main_test(const int *v, size_t n)
{
int *v1 = memcpy(malloc(n * sizeof(int)), v, n * sizeof(int));
evenodd(v1, n);
if (is_evenodd(v1, n)) {
printf("Ok!\n");
}
else {
printf("Fail!\n");
}
free(v1);
}
int main(void)
{
main_test((int[]) { 1 }, 0);
main_test((int[]) { 1 }, 1);
main_test((int[]) { 2 }, 1);
main_test((int[]) { 1, 2 }, 2);
main_test((int[]) { 1, 3 }, 2);
main_test((int[]) { 2, 1 }, 2);
main_test((int[]) { 2, 4 }, 2);
main_test((int[]) { 1, 3, 2 }, 3);
main_test((int[]) { 1, 4, 2 }, 3);
size_t n = 1000;
int *a = malloc(n * sizeof *a);
for (size_t i = 0; i < n; ++i) {
a[i] = rand();
}
main_test(a, n);
free(a);
return 0;
}
You can try the following code :
int even_index = 0; //start index
int odd_index = 4; //end index
for(int i=0;i<5;i++){
if(ptr[i] % 2 == 0){
int temp = ptr[even_index];
ptr[even_index++] = ptr[i]; //swapping values and incrementing even_index
ptr[i] = temp;
}else{
int temp = ptr[odd_index];
ptr[odd_index--] = ptr[i];
ptr[i] = temp;
}
}
or you can also count the number of even numbers in the digits during input and assign odd_value = even_num // number of even digits
ok so I solved it....
see the even numbers always end up in even indexes so we need to set a pointer on those even index(current index) and search for any even number after the current index.
if we find any(even number) we swap the current index value with the even number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n,j,i,num,v;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Entered number of elements: %d\n", n);
ptr = (int*)malloc(n * sizeof(int));
for (i = 0; i < n; ++i) {
scanf("%d", &v);
ptr[i] = v;
}
i=0;
j=0;
while(i<n && j<n){
if (ptr[j]%2==0){
num=ptr[i];
ptr[i]=ptr[j];
ptr[j]=num;
i+=2;
j=i;
}
j++;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
If this problem is to sort an array in order (descending) and then further place all even values before odd values I would recommend:
Sort the array
Swap and shift any odd numbers with the even numbers
Here's a naive implementation:
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (arr[i] < arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
int lastEven = 0;
for (int i = 0; i < len - 1; i++) {
if (arr[i] % 2 && (arr[i + 1] % 2 == 0)) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
lastEven = i;
} else if (arr[i] % 2 == 0 && lastEven-i > 1) {
for (int j = i; j > lastEven; j--) {
tmp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = tmp;
}
lastEven++;
}
}
Given the input [13,11,10,17,18] this will first sort the array ([18,17,13,11,10]) then separate the evens and odds ([18,10,17,13,11])
I am a beginner in C and was working on a program that can output the prime numbers within the range using Eratosthenes' sieve. However, the program does not output anything and is in a loop, I believe this has to do with the repetitive use of for statements in my code but also have no idea where I may be going wrong. I tried putting break statements with if statements but it seems like it does not work. Thank you in advance.
#include <stdio.h>
#include <math.h>
int sieve(int limit);
int main() {
int maximum = 0;
printf("Enter the limit of prime numbers:");
scanf_s("%d", &maximum);
sieve(maximum);
return 0;
}
int sieve(int limit) {
int array[100];
int common = 2;;
for (int i = 0; i <= limit; i++) {
array[i] = i + 1;
}
for (;;) {
for (int j = 0; j <= limit; j++) {
if (array[j] % common == 0 && !(array[j] == common)) {
array[j] = 0;
array[0] = 0;
}
}
for (int k = 0; k <= limit; k++) {
if (!(array[k] == 0) && !(common == array[k])) {
common = array[k];
break;
}
}
if (common >= sqrt(limit))
break;
}
for (int o = 0; o < limit; o++) {
if (!(array[o] == 0)) {
printf("%d ", array[o]);
}
}
return 0;
}
I hope I'm not out of my depth here since it's been a while since I have touched C.
You could set a boolean variable to false. Within the child loop you set it to true. Add an if statement within the parent loop to check if that variable is true and break the parent loop if it is.
Quick pseudo-code:
bool t = false
parent loop:
child loop:
if something
t = true
if t
break
The outer loop runs for ever because you do not increment common inside the loop body.
Note also that you can enumerate all multiples of common with a simple addition instead of a costly % operator.
Here is a modified version:
#include <stdio.h>
int sieve(int limit);
int main() {
int maximum = 0;
printf("Enter the limit of prime numbers:");
scanf_s("%d", &maximum);
sieve(maximum);
return 0;
}
int sieve(int limit) {
int array[limit + 1];
for (int i = 0; i <= limit; i++) {
array[i] = i;
}
for (int common = 2; common * common <= limit; common++) {
// remove all multiples of common. Start at common*common
// because lesser multiples have already been removed as they
// are multiples of a smaller number
for (int i = common * common; i <= limit; i += common) {
array[i] = 0;
}
}
for (int i = 1; i <= limit; i++) {
if (array[i] != 0) {
printf("%d ", array[i]);
}
}
printf("\n");
return 0;
}
Note that you can use an array of unsigned char initialized to 1 and print i instead of array[i].
Can someone help me to figure out why my code is unable to accurately find the duplicate of elements?
#include <stdio.h>
int main() {
int array[10];
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < 10; i++) {
scanf_s("%d", &array[i]);
}
for (int i = 0; i < 10; i++) {
for (int j = i + 1; j < 10; j++) {
if (array[i] == array[j]) {
count++;
break;
}
}
}
printf("The duplicates are : %d ", count);
}
I'm a beginner at this language so any advice and suggestions to help me solve this exercise will be much appreciated.
First of all the first loop runs 10 times even if the user enters less numbers. You can fix that by doing:
for (int i = 0; scanf_s("%d", &array[i]) == 1 && i < 10; i++);
Then the logic of the other two loops is wrong. I initially got wrong what you meant. I thought you wanted to know how many times a number is duplicated. So I wrote the wrong program and then modified it for your purposes. Here is your program:
#include <stdio.h>
int main() {
int n[10];
int dupes[5], d = 0;
int flag = 1, omg;
for ( omg = 0; scanf("%d", &n[omg]) == 1 && omg < 10; omg++);
for (int i = 0; i < omg; i++) {
for (int j = i+1; j < 10; j++) {
if( n[i] == n[j] ) {
if( d > 0 ) {
for(int k = 0; k < d; k++) {
if( n[i] == dupes[k] ) {
flag = 0;
break;
}
}
}
if( flag ) {
dupes[d] = n[i];
++d;
break;
}
else {
flag = 1;
break;
}
} // end outer if
}
}
printf("There are %d numbers that have at least one dupe\n", d);
return 0;
}
I named a variable omg out of desperation, writing this program was a nightmare. (Because it came from the ashes of a previous program)
Your code correctly determines the number of duplicate entries in the array.
If instead you want to determine the number of duplicated values, you must modify the algorithm:
#include <stdio.h>
int main() {
int array[10] = { 0 };
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < 10; i++) {
scanf_s("%d", &array[i]);
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (array[i] == array[j]) {
if (i < j)
count++;
if (i != j)
break;
}
}
}
printf("There are %d duplicate values\n", count);
return 0;
}
I use a structure 'Number' which contains the number and its duplicate, then I fill the array and I put it in ascending order then I calculate the number of duplicate of each number and I fill in the strecture like this :
my code:
#include <stdio.h>
#define size 10
typedef struct Number
{
int number;
int duplicate;
}Number;
int main()
{
int array[size];
Number array2[size];
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
int temp=size;
int temppppp=0;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(array[i]>array[j])
{
temppppp=array[i];
array[i]=array[j];
array[j]=temppppp;
}
}
}
printf("\n\n");
for (int i = 0; i < size; i++)
{
printf("[%d]",array[i]);
}
printf("\n\n");
int i=0;
int j=0;
while(i<size)
{count=1;
while(i<(size-1)&&array[i]==array[i+1])
{
count++;
i++;
}
if(count>=2)
{
array2[j].number=array[i-1];
array2[j].duplicate=count;
j++;
}
i++;
}
int p=0;
while(p<j)
{
printf("\n[%d] has duplicated %d times !\n",array2[p].number,array2[p].duplicate);
p=p+1;
}
printf("\n\n");
printf("\nThere are %d duplicate values\n", j);
}
I am working on a program that removes duplicate values from an array by ordering it and then removing duplicate, consecutive values. First I execute a selection sort sorting method, and then call a function removedup() that modifies the array and returns the size. Then I basically print the values in the array up to that size. However, when I execute it, it only prints the original array and then a bunch of blank space. Does anyone know why this is occurring?
My code: http://pastebin.com/uTTnnMHN
Just the de-duplication code:
int removedup(int a[])
{
int i, count, j;
count = n;
for (i = 0; i < (n - 1); i++) {
if (a[i] == a[i + 1]) {
for (j = 0; j < (n - i); j++) {
a[j] = a[j + 1];
}
count--;
i--;
}
}
return count;
}
-1 for(j=0;j<(n-i);j++)
Is your loop to shift left your array (thus removing the duplicate value), j should not be init to j but to i, and the condition does not seem right
A correct one could be:
for(j=i;j<n-1;j++)
{
a[j]=a[j+1];
}
a[n-1] = 0; // Because you shift your table to the left, the last value should not be used
first if i=0 and if a[i]==a[i+1] then i=-1
for(i=0;i<(n-1);i++)
{
if(a[i]==a[i+1])
{
for(j=0;j<(n-i);j++)
{
a[j]=a[j+1];
}
count--;
i--; //i=-1 if(a[i]==a[i+1]) && if(i==0)
}
}
In your duplicate removal function, you need to start the moving loop at i, as has been mentioned, and you must use count - 1 as the loop bound for both loops, otherwise you will have an infinite loop whenever there are duplicates, because then a[n-2] == a[n-1] always after the first moving loop.
int removedup(int a[])
{
int i, count, j;
count = n;
for(i = 0; i < (count-1); i++)
{
if(a[i] == a[i+1])
{
for(j = i; j < (count-1); j++)
{
a[j]=a[j+1];
}
count--;
i--;
}
}
return count;
}
works correctly.
Since you're creating another array anyway, why not simplify your function?
int removedup(int a[], int b[])
{
int i;
int count = 1;
b[0] = a[0]
for(i=1;i<(n-1);i++){
if(a[i-1] != a[i]){
b[count++] = a[i];
}
}
return count;
}
Then when you call the function,
count=removedup(a, OutArray);
int removedup(int a[])
{
int i;
count = n;
for (i = 0; i < (count-1); ++i) {
if (a[i] == a[i+1]) { /* found a duplicate */
int d = 0; /* count the duplicates */
do {
++d;
} while ((i+1+d) < count && a[i] == a[i+1+d]); /* probe ahead again for a duplicate */
/* at this point we have either run out of duplicates or hit the end of the array */
if ((i+1+d) < count)
memmove(&a[i+1], &a[i+1+d], sizeof(int)*(count-(i+1+d))); /* shift down rest of the array if there's still elements to look at */
count -= d; /* adjust the count down by the number of duplicates */
}
}
return count;
}
what about this one,without sort but traverse.finally print the effective values of the array and return its size.
int removedup(int a[])
{
int i, count, j;
count = n;
int b[n];
memset(b,0,sizeof(b));
for (i = 0; i < (n - 1); i++)
{
if (-1 != b[i])
{
for(j=i+1;j<n-1;j++)
{
if( a[i]==a[j])
{
b[j]=-1;
count--;
}
}
}
}
for(i=0;i<n-1;i++)
{
if(-1 != b[i])
{
printf("%d ",a[i]);
}
}
return count;
}