C language tricky good pointers questions answers and explanation operators data types arrays structures questions functions recursion preprocessors, looping, file handling, strings questions switch case if else printf advance c linux objective types mcq faq interview questions and answers with explanation and solution for freshers or beginners. Placement online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c c++ tutorials and pdf

this is more easy.....
ReplyDelete#include
#include
void main()
{
clrscr();
int a[5];
int n=5,i,j,k;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n=n-1;
}
}
}
for(i=0;i<n;i++)
printf(" %d ",a[i]);
getch();
}
its easy thnks...
Deletei didnt revome more than one duplication
DeleteI tried this code did not remove more than one duplicate .please anyone explain
Deletethis code doesn't work as a[k+1] is not defined for k=n-1.
ReplyDeletethis program does not work for more then one duplicate element..........
Deletewho told this doesn't works?it works perfecctly
ReplyDeleteIt works fine no issues.
ReplyDeleteplease anyone can explain this
ReplyDeletei cant understand why he took k variable here
ReplyDelete#include
ReplyDeletevoid main()
{
int a[20],i,j,m,n;
printf("enter no. of elements: ");
scanf("%d",&n);
printf("\nNow, enter the elements: ");
for(i=0;i1)
for(j=i;j<n-1;j++)
a[j]=a[j+1];
n--;
}
printf("\nThe array after removing duplicates is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
I dont think the first program can remove a no repeating 3 times
ReplyDeletecan someone give me solution in o(n) or less than o(n2) time.
ReplyDeleteplz replyyyy
very nice program sweatab malvia.............
ReplyDeleteThis comment has been removed by the author.
ReplyDeletethe program below is very good to prevent duplicate entry in an array:
ReplyDelete#include
#include
void main()
{
int a[50],i,j=0,n,t;
printf("enter the size=");
scanf("%d",&n);
printf("enter values=");
for(i=0;i=0)
{
if(a[i]==a[j])
{
printf("u have already enter the number=%d\n",a[i]);
printf("enter new value=");
scanf("%d",&a[i]);
}
j=j-1;
}
}
for(i=0;i<n;i++)
{
printf("numbers=%d\n",a[i]);
}
getch();
}
swetabh malviya's program worked correctly...thank u
ReplyDeletereally its a good one awetabh!
ReplyDeleteswetabh malviya's Program doesnt work for these test case:
ReplyDeleteLength : 5 array : 11211
Length : 5 array : 11123
And Many More....
But guys,the solutions are in n^2 complexity...
ReplyDeletednxxx dudes.
ReplyDelete#include
ReplyDelete#include
void main()
{
int a[10],i,j,n;
printf("enter the no of elements");
scanf("%d",&n);
printf("enter the array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("original array");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
j=0;
for(i=1;i<n;i++)
{
if(a[i]!=a[j])
{
j++;
a[j]=a[i];
}
}
n=j+1;
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
getch();
}
one way is to sort the array using best sorting algo.
ReplyDeletetake a variable that compares each value to its previous value.
Scan array and copy each different element onto a new array
new array would have all duplicate elements removed
time cmplexity:O(nlogn) if used merge/quick/heap sort
int arrayUnique(int array[], int size) {
ReplyDeletefor ( int i = 0; i < size; i++ ) {
for ( int j = i + 1; j < size; j++ ) {
if ( array[i] == array[j] ) {
size -= 1;
for ( int k = j; k < size; k++ ) {
array[k] = array[k+1];
}
j -= 1;
}
}
}
return size;
}
It works!
DeleteTo give an answer in less than O(n^2), first sort the array using any known sorting algo(for eg. merge-sort) which requires a time complexity of O(nlogn) and then check every element with the element on its right for duplicity.
ReplyDeleteSo, overall it requires a time complexity of O(nlogn)
another simple code for deletion of duplicate elements in an array....
ReplyDelete#include
#include
void swap(int a[],int,int);
void main()
{
int n,a[20],i,j;
printf("Input the number of elements...");
scanf("%d",&n);
printf("\nInput the elements...");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Elements before deletion...");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=(i+1);j<n;j++)
{
if(a[i]==a[j])
{
swap(a,j,n);
j=j-1;
n=n-1;
}
}
}
printf("\nElements after deletion...");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
void swap(int a[],int j,int n)
{
while(j<(n-1))
{
a[j]=a[j+1];
j++;
}
}
/*this is for to remove duplicate elements from array*/
ReplyDelete#include
int main()
{
int a[8]={11,3,4,3,1,6,3,4};
int n=8,i,j,k;
printf("array element are:");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
a[j]=0;
}
}
}
printf("\nafter deleting duplicate element are:");
for(i=0;i<n;i++)
{
if(a[i]==0)
{
continue;
}
else
{
printf("%d\t",a[i]);
}
}
return 0;
}
This one works
ReplyDelete#include
void main(){
int a[30],n,h,i,j,k,l,x;
printf("Enter number of elements in the array:");
scanf("%d",&n);
printf("Enter the elements of the array:");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(h=0;h<2;h++){
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]==a[j]){
for(k=j;k<n-1;k++){
a[k]=a[k+1];
}
n=n-1;
}
}
}
}
printf("Now the array is:\n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
}
#include
ReplyDeleteint main()
{
int x[10]={8,3,5,4,4,3,1,8,5,9},n=10,i,k,t,count=0,j;
for(i=0;i0)
{
i--;
n--;
}
}
}
n=n-count;
printf("the array is:");
for(i=0;i<n;i++)
{
printf("%d",x[i]);
}
}
can u correct my logic
ReplyDelete#include
ReplyDeleteint main()
{
int x[10]={8,3,5,4,4,3,1,8,5,9},n=10,i,k,t,r,count=0,j;
for(i=0;i<n;i++)
{
k=i;
for(j=k+1;k<n;j++)
{
if(x[k]==x[j])
{
x[k]=x[j];
for(t=j+1;t<n;t++)
{
x[j]=x[t];
}
n--;
count++;
}
}
}
r=n-count;
printf("the array is:");
for(i=0;i<n;i++)
{
printf("%d",x[i]);
}
}
can u tel me whr is wrng
ReplyDeleteEven swetha's code doesn't work....take input as "1,1,1,1,2,2,3,4,5,5". output is "1,1,2,3,4,5".
ReplyDeleteIt is easy if u take a flag and take an array. Initialize the first element of the new array to the input array and check if elements. If the elements are equal, then make flag as '0', initially flag is '1'. if the element is not equal then insert the element to the new array in the next position and continue. This even requires two for loops. and the condition for second for loop is till the size of the new array.
#include
ReplyDelete#include
int main ()
{
int n,a[100], b[100],i,j,count=0;
printf("How many no. do you want to enter ?");
scanf("%d",&n);
printf("Enter %d no.s of the array ",n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for (j=0;j<count;j++)
if (a[i]==b[j]) break;
if (j==count)
{
b[count]=a[i];
count++;
}
}
printf("\nthe array elements after deleting duplicates is :");
for (i=0;i<count;i++)
printf("\n%d",b[i]);
getch();
}
helpfull
ReplyDelete