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

Explain about Garbage Collector?

0 Answers  


What is jump statement in C++?

0 Answers   Ericsson,


Explain the operation of overloading of an assignment operator.

0 Answers  


What are function poinetrs? where are they used?

1 Answers   CTS,


What are c++ redistributables?

0 Answers  






. If employee B is the boss of A and C is the boss of B and D is the boss of C and E is the boss of D. Then write a program using the Database such that if an employee name is Asked to Display it also display his bosses with his name. For eg. If C is displayed it should also display D and E with C?

0 Answers  


If horse and bird inherit virtual public from animal, do their constructors initialize the animal constructor? If pegasus inherits from both horse and bird, how does it initialize animal’s constructor?

0 Answers  


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

0 Answers  


What is the use of volatile variable?

0 Answers  


the maximum length of a character constant can be a) 2 b) 1 c) 8

0 Answers  


How can we read/write Structures from/to data files?

0 Answers  


Why preincrement operator is faster than postincrement?

5 Answers  


Categories