In: Computer Science
(Duplicate Elimination) Use a one dimensional array to slove the following problem. Read 20 numbers, each of which is 10 and 100, inclusive. As each number is read, print it only if it's not a duplicate of a number already read. Provide for the "worst case" in which all 20 numbers are different. use the smallest possible array to slove this problem.
Add the following functionality to your solution. Sort the array in ascending order and print the elements of the sorted array.
if you could please explain each line of the code that would be wonderful.
I think you are saying input number should be between 10 and 100, inclusive.
I have Written code is in C++, as you have not specified any specific programming language.
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
//array with size 20
int arr[20];
cout<<"input 20 integers one-by-one : ";
for(int i = 0; i < 20; i++)
{
cin>>arr[i];
int flag = 0;
//This loop checks if given entered element is already exist in array or not
for(int j = 0; j < i; j++)
{
//if it already exist then set flag to 1
if(arr[i] == arr[j])
flag = 1;
}
if(flag == 0)
{
cout<<arr[i]<<"\n\n";
}
else cout<<"recently entered Number is duplicate."<<"\n\n";
}
//sort is method to sort array in c++
sort(arr, arr + 20);
//print sorted array
for(int i = 0; i < 20; i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
Output for a sample testcse:
Try running this code on your own system.
Like, if this helped :)