-4

Edit: I need to maintain the order in which the elements are present in the original array, so sorting won't work.

I have a 1-D array containing some elements and I am using printf() in C but I only want to print an element if and only if it has not already been printed before.

I am thinking of using nested loops to compare if the element I am about to print from the current position in the array was already present in a lower index of the array but it's not working. What am I missing? Or is my whole approach wrong?

So far, I have tried this, which is not working:

int arr[20];

After this I take user input for no. of elements in p and of course, p<20. Then, user enters the elements one by one. I use scanf() for this.

for(i=1;i<=p;i++)
{
    for(j=i+1;j<=p;j++)
    {
        if(arr[i]!=arr[j])
        {
            printf("%d",arr[j]);
        }
    }
}
Anibha
  • 7
  • 2
  • _"There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs."_ – Sourav Ghosh Jan 04 '16 at 15:30
  • 1
    first thing to ask yourself: "How would *I* do that"? (I wonder who is upvoting this..) – Eugene Sh. Jan 04 '16 at 15:33
  • What type of elements are you printing? Can you show us an attempt at this? – Veltas Jan 04 '16 at 15:36
  • Hint: Working with *sorted* array will help... – Eugene Sh. Jan 04 '16 at 15:38
  • Simple. Sort the array and print the first element. Then, for all remaining elements, print them if they're not the same as the previous element. – enhzflep Jan 04 '16 at 15:38
  • i have edited the question and included my current approach – Anibha Jan 04 '16 at 15:38
  • @enhzflep i need to print the elements in the same order in which they appear in the array, so sorting won't help. – Anibha Jan 04 '16 at 15:40
  • @Veltas i have edited the question to include code and the elements are positive integers. – Anibha Jan 04 '16 at 15:42
  • @EugeneSh. i need to print the elements in the same order in which they appear in the array, so sorting won't help. – Anibha Jan 04 '16 at 15:45
  • Please include the declaration of the array in the code. – Veltas Jan 04 '16 at 15:48
  • @Anibha - oh :( - well, in that case lets just slow down and have a bit more of a think. (a) we only care if the number has been seen before. Therefore, we can use the current value of i as the maximum value of j (we dont want to avoid printing if the number appears again, later, only before) (b) We don't need to continue checking if the current number has been seen before - we can break out of the inner loop at this point. By using a flag called say, 'alreadySeen` we can tell if we exited the loop after checking all options and should print the number, or if the number should be ignored. – enhzflep Jan 04 '16 at 16:00
  • @enhzflep I tried what you said. It won't work! T_T – Anibha Jan 04 '16 at 16:16
  • @Veltas edited to include declaration. – Anibha Jan 04 '16 at 16:19
  • @Anibha - I've done it too. It _does_ work - perhaps I've misread your code and given directions based on what I _thought_ I was looking at. In any case, it works.If the question is re-opened, I'll consider posting it. Renaming your variables would be a good move - giving them meaningful names often (read: nearly always) makes the code easier to think-through. I suggest `numElems` instead of `p`, `curPrintIndex` instead of `j` and `curCompareIndex` instead of `i` – enhzflep Jan 04 '16 at 16:31
  • 5
    Possible duplicate of [Algorithm: efficient way to remove duplicate integers from an array](http://stackoverflow.com/questions/1532819/algorithm-efficient-way-to-remove-duplicate-integers-from-an-array) – GingerPlusPlus Jan 04 '16 at 16:57

2 Answers2

1

You need to check all previous items before you know if the item has already occurred.

#include <stdio.h>
#include <stdlib.h>

int compareFunc(const void *op1, const void *op2 )
{
    int *a, *b;
    a = (int*)op1;
    b = (int*)op2;
    return *a - *b;
}

void printUnique(int *array, int numElems)
{
    int curPrintIndex, curCompareIndex;
    char alreadySeen;

    for (curPrintIndex=0; curPrintIndex<numElems; curPrintIndex++)
    {
        alreadySeen = 0;
        for (curCompareIndex=0; curCompareIndex<curPrintIndex; curCompareIndex++)
        {
            if (array[curCompareIndex] == array[curPrintIndex])
            {
                alreadySeen = 1;
                break;
            }
        }
        if (alreadySeen == 0)
            printf("%d\n", array[curPrintIndex]);
    }
}

int main()
{
    const int numItems = 100;
    int *array, i, lastVal;

    array = calloc(numItems, sizeof(int) );

    for (i=0; i<numItems; i++)
        array[i] = rand()%numItems;

    printUnique(array, numItems);

    free(array);
    return 0;
/*
    qsort(array, numItems, sizeof(int), compareFunc);

    printf("%d\n", array[0]);
    lastVal = array[0];

    for (i=1; i<numItems; i++)
    {
        if (array[i] != lastVal)
        {
            lastVal = array[i];
            printf("%d\n", array[i]);
        }
    }
*/
}
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
enhzflep
  • 12,927
  • 2
  • 32
  • 51
0
#include <stdio.h>

int main(void){
    int arr[20] = { 4,8,4,2,4,8,1,3,2,7 };//the elements are positive integers.
    int i, j, p = 10;

    for(i=0;i<p-1;i++){
        if(arr[i] < 0)
            continue;
        for(j=i+1;j<p;j++){
            if(arr[j] > 0 && arr[i]==arr[j])
                arr[j] *= -1;
        }
    }
    for(i=0; i < p; ++i)
        if(arr[i] > 0)
            printf("%d ", arr[i]);
    puts("");
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70