Solved programming problem

Replace the duplicate value of 1D array by (-1) in C.

Source code

#include<stdio.h>

int main()

{

    int array[100],array1[100];

    int i,j,n;

    printf("Please Enter The  array size: ");

    scanf("%d",&n);

    printf("
Please Enter %d elements of the array: 
",n);

    for(i=0; i<n; i++)

    {

        scanf("%d",&array[i]);

    }

    for(i=0; i<n; i++)

    {

        for(j=i+1; j<n; j++)

        {

            if(array[i]==array[j]) ///checking is any same value available or not

            {

                array[j]=-1; ///replacing the value as -1.

            }

        }

    }

    printf("New array is: 
");

    for(i=0; i<n; i++)

    {

        printf("%d ",array[i]);

    }

    printf("
");

    return 0;

}

Output

Please Enter TheĀ  array size:5

Please Enter 5 elements of the array: 1 3 1 2 1

New array is:1 3 -1 2 -1

Join the discussion

Add your comment