Question

Explain the space-time tradeoff implemented in efficient hash tables? b) Use hashing to design and algorithm...

Explain the space-time tradeoff implemented in efficient hash tables? b) Use hashing to design and algorithm to identify all duplicate elements in an array, e.g., for the following array [ “r6i”, “opd”, “r6i”, “ydg”, “x5s”, “pc1”, “tni”, “594”, “ wi5”, “ip0”, “vvj”, “oad”, “ydg”, “‘opd” ] the elements [“r6i”, “opd”, “ydg”] are duplicates. The time complexity of the algorithm must be O(n)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In hash tables time complexity will be of o(n) but space for elements will be high for stroing them,we can acess the required element in O(1) time

Here is the algorithm code in cpp:-

Explanation:-First we create a hash table of (unordered map) of string,int ,by default values will be zero.we traverse through the entire array and increse the count for the element by 1,if we encounter same element the count will increase .At last we traverse through th enitire hash if count >1 we will print element corresponding to that element.

#include<bits/stdc++.h>
using namespace std;
#include <unordered_map>
#include<iostream>
int main(){
   string arr[]={"r6i", "opd", "r6i", "ydg", "x5s", "pc1", "tni", "594", "wi5", "ip0", "vvj", "oad", "ydg", "opd" };
   unordered_map<string,int> hash;
int len=sizeof(arr)/sizeof(arr[0]);
   for(int i=0;i<len;i++){
       hash[arr[i]]++;
   }

   cout<<"print duplicates:"<<endl;
   for(auto x:hash){
       if(x.second>1){
           cout<<x.first<<" ";
       }
   }
   cout<<endl;
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Explain the space-time tradeoff implemented in efficient hash tables? b) Use hashing to design and algorithm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this project, you will work on the algorithm (discussed in Module 1) to determine the...

    In this project, you will work on the algorithm (discussed in Module 1) to determine the length of the longest sub sequence of consecutive integers in an array You will implement the algorithm using Hash tables. You are provided with sample code (in C++ and Java) representing the linked list-based implementation of Hash tables (as an array of Linked Lists). You could go through the code to understand the implementation of a Hash table and the functions that can be...

  • A. What is the time complexity of Insertion Sort? B. Explain why Insertion Sort has the...

    A. What is the time complexity of Insertion Sort? B. Explain why Insertion Sort has the time complexity you listed above in Part A. C. Show the steps followed by the Quicksort algorithm given below in pseudocode when sorting the following array. Algorithm Quicksort (A, left, right) if (left < right) pivot Point + [(left+right)/2] // note central pivot it left - 1 j right + 1 do do iti + 1 while (i < A.size) and (A[i] = A[pivotPoint])...

  • 1 Objective Build a hashing algorithm that is suitable for use in a Bloom Filter. Please...

    1 Objective Build a hashing algorithm that is suitable for use in a Bloom Filter. Please note that while a cryptographic hash is quite common in many Bloom Filters, the hashing algorithm to be implemented is a mix of the the following algorithmic models, specifically, a multiply & rotate hash colloquially known as a murmur hash, and an AND, rolale, & XOR hash colloquially known as an ARX hash. 2 Requirements • Inputs. Read the input file which contains strings...

  • Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...

    Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...

  • == Programming Assignment == For this assignment you will write a program that reads in the...

    == Programming Assignment == For this assignment you will write a program that reads in the family data for the Martian colonies and stores that data in an accessible database. This database allows for a user to look up a family and its immediate friends. The program should store the data in a hashtable to ensure quick access time. The objective of this assignment is to learn how to implement and use a hashtable. Since this is the objective, you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT