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 ].
Answer Posted / 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 View All Answers
Is swift better than c++?
Does c++ have string data type?
What is the array and initializing arrays in c++?
Can you help me with this one? Make a program that when a user inputed a Product Name, it will display its price, and when the user inputed the quantity of the inputed product, it will show its total price. The output must be like this: Product Name: Price: Quantity: Total Price: ..this is the list of products to be inputed: Cellphone - 1500 Washing Machine - 5200 Television - 6000 Refrigirator - 8000 Oven - 2000 Computer - 11000 thanks..:D
What are static member functions?
What are the comments in c++?
What is pure virtual function?
State the difference between pre and post increment/decrement operations.
What is &x in c++?
What is meant by forward referencing and when should it be used?
When one must use recursion function? Mention what happens when recursion functions are declared inline?
How do you invoke a base member function from a derived class in which you’ve overridden that function?
What is difference between array and vector in c++?
What is algorithm in c++ programming?
What is an adaptor class or wrapper class in c++?