Question

In: Computer Science

Take advantage of the fact that the range of the integers in the list is fixed...

  • Take advantage of the fact that the range of the integers in the list is fixed (0 to m, where m is the largest ID you can find in the linked list). Use a boolean array seen of length m+1 to indicate if elements in the array have been seen before. Then determine if there are duplicates by performing a single pass through the unsorted list. Hint: while traversing the list, seen[item] = True if integer item has been seen before in the search.
  • 0
    1
    15
    112
    5000
  • 150
    112
    5000

Solutions

Expert Solution

As you have not mentioned the language hence I am using C++
Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

#include<bits/stdc++.h>
using namespace std;
int main()
{
//number of elements in list
int n = 10;

//unsorted list
int arr[10] = {10, 34,65, 123,6, 12, 10, 43, 6};
cout<<"This is a list of input elements"<<endl;
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";

int maximum=arr[0];

//Find the maximum value from the array
for(int i=0;i<n;i++)
{
if(maximum<arr[i])
maximum=arr[i];
}

//Generate boolean array of size maximum+1
bool seen[maximum+1];

//Initialize seen array by false
for(int i=0;i<maximum+1;i++)
seen[i] = false;

cout<<endl<<"This is a list of all duplicate elements"<<endl;
//Traverse the list to check duplicate elements
for(int i=0;i<n;i++)
{
//This element is encountered for the first time
if(seen[arr[i]] == false)
seen[arr[i]] = true;

//This is a duplicate element has print it
else
cout<<arr[i]<<endl;
}
}

Below is the screenshot of output

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.


Related Solutions

Create a Python program that will take an unsorted list of 1000 integers and sort them...
Create a Python program that will take an unsorted list of 1000 integers and sort them using a bubble sort and an insertion sort. Your output should include displaying both sorted lists (from each algorithm) identifying each sorted list by the algorithm used.
The fact that BE/A is greatest for A near 60 implies that the range of the...
The fact that BE/A is greatest for A near 60 implies that the range of the nuclear force is about the diameter of such nuclides. (a) Calculate the diameter of an A = 60 nucleus. ________ fm (b) Calculate BE/A for 56Fe and 102Ru. The first is one of the most tightly bound nuclides, while the second is larger and less tightly bound. 56Fe ________ MeV 102Ru ________ MeV
Write a program that will sum the integers between a given range (limit your range from...
Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the range 0 to 50. Remember to create the...
Give an example of a RANGE partition for a fact table of a data warehouse and...
Give an example of a RANGE partition for a fact table of a data warehouse and its major advantage.
college take the advantage of Solar energy
college take the advantage of Solar energy
Because of the fact that the terms of an insurance contract are fixed by the insurer...
Because of the fact that the terms of an insurance contract are fixed by the insurer instead of being determined by a bargaining process, the insurance policy is said to be 1. a contract of utmost good faith. 2. an aleatory contract. 3. a unilateral contract. 4. a contract of adhesion.
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
The range of integers that can be represented by this 12-bit construction is -255 to 255...
The range of integers that can be represented by this 12-bit construction is -255 to 255 (a 9-bit signed integer has a range of -256 to 255). What are the floating-point representations for the first eight, odd, positive, decimal integers (i.e., 1, 3, 5, 7, 9, 11, 13, 15) in this 12-bit notation?
It is desired to take advantage of the current of a river coming upstream for an...
It is desired to take advantage of the current of a river coming upstream for an electric power generation project. The point where the turbine-generator will be installed is 50 meters below the upstream level. The upstream speed is 9 m / s and the downstream speed is 2 m / s. There will be a mass flow of water of 1500 kg / s. Calculate an estimate for the generation of electrical power produced if you have a turbine...
Then why don't the parents of sea urchins take care of the young? [In fact the...
Then why don't the parents of sea urchins take care of the young? [In fact the adults do in a way. After the embryo has grown large enough and metamorphosed into a young urchin they hide in the spines of the adults, living off of the scraps of food produced from the messy eating of the adults
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT