Given an array of size N in which every number is between 1 and
N, determine if there are any duplicates in it. You are allowed
to destroy the array if you like. [ I ended up giving about 4 or
5 different solutions for this, each supposedly better than the
others ].



Given an array of size N in which every number is between 1 and N, determine if there are any dupli..

Answer / sujan_faith

<!--i have just replaced the same array element with
the value zero but alse we can destroy that element-->>


#include<iostream>
#define size 10
using namespace std;

int main()
{
int a[10]={10,10,10,2,1,1,1,1,1,2};
int j=1;
for(int i=0;i<size;i++)
{
for(int k=i+1;k<size;k++)
{
if(a[i]==a[k]&&a[i]!=0)
{
a[k]=0;
j++;

}
if(k==(size-1)&& a[i]!=0)
cout<<"Value is: "<<a[i]<<" which is repeated "<<j<<" times"<<endl;
}
j=1;
}
system("pause");
}

Is This Answer Correct ?    8 Yes 0 No

Post New Answer

More C++ General Interview Questions

Write a program to encrypt the data in a way that inputs a four digit number and replace each digit by (the sum of that digit plus 7) modulus 10. Then sweep the first digit with the third, second digit with the fourth and print the encrypted number.

0 Answers   CIIT Wah,


How do you generate a random number in c++?

0 Answers  


What is data abstraction? How is it different from data encapsulation?

0 Answers  


Is it possible to provide special behavior for one instance of a template but not for other instances?

0 Answers  


What do you mean by pure virtual functions in C++? Give an example?

1 Answers  






What is class and structure in c++?

0 Answers  


What is lazy initialization in c++?

0 Answers  


what are Access specifiers in C++ class? What are the types?

0 Answers  


What is the fastest c++ compiler?

0 Answers  


What is data type in c++?

0 Answers  


What is stack unwinding?

0 Answers  


How does class accomplish data hiding in c++?

0 Answers  


Categories