10A Implementing Dictionaries by Map
10A Implementing Dictionaries by Map
Cosmin E. Oancea
[email protected]
(3) for all the words in a text, count up how many times each
word appears in the text.
Motivation
If we use (unsorted) arrays, what is the worst-time complexity of
the following programs:
(1) check whether a word is valid, i.e., it appears in a dictionary;
(3) for all the words in a text, count up how many times each
word appears in the text.
Answers:
(1) linear time in the size of the dictionary, i.e., O(n) on average
Run the prefix-sum example;
l e t c o n t a c t s = new Map ( ) ;
c o n t a c t s . s e t ( ’ Sarah ’ , {phone : ” 25551234 ” , c i t y : ” Aa r h u s ” } ) ;
c o n t a c t s . has ( ’ Sarah ’ ) ; / / t r u e
c o n t a c t s . g e t ( ’ Anna ’ ) ; / / u n d e f i n e d
c o n t a c t s . s e t ( ’ Anna ’ , {phone : ” 72344321 ” , c i t y : ” Odense ” } ) ;
c o n t a c t s . s e t ( ’ Sarah ’ , {phone : ” 25555555 ” , c i t y : ” Aa r h u s ” } ) ;
c o n t a c t s . g e t ( ’ Sarah ’ ) ; / / { phone : ”2 5 5 5 5 5 5 5 ” , c i t y : ” Aa r h u s ”}
co n t a ct s . delete ( ’ Jens ’ ) ; / / f a l s e
c o n t a c t s . d e l e t e ( ’ Sarah ’ ) ; / / t r u e
co n s o l e . l o g ( c o n t a c t s . s i z e ) ; / / 1
This way of setting properties does not interact well with the Map
data structures, and you will get easily confused:
What Not to Do!
Do not add new fields to a Map object other than with set.
l e t wrongMap = new Map ( ) ;
wrongMap [ ’ bla ’ ] = ’ blaa ’ ;
wrongMap [ ’ bla2 ’ ] = ’ blaaa2 ’ ;
This way of setting properties does not interact well with the Map
data structures, and you will get easily confused:
wrongMap . has ( ’ bla ’ ) // false
wrongMap . d e l e t e ( ’ bla ’ ) // false
co n s o l e . l o g ( wrongMap ) / / Map { b l a : ’ blaa ’ , bla2 : ’ blaaa2 ’ }
Iterating through a Map
Elements are iterated in the same order as they were inserted.
(This could be useful, for example for removing duplicates.)
l e t myMap = new Map ( ) ;
myMap . s e t ( 9 , ’ nine ’ ) ;
myMap . s e t ( 4 , ’ four ’ ) ;
myMap . s e t ( 5 , ’ f i ve ’ ) ;
f o r ( l e t [ key , va l u e ] o f myMap ) {
co n s o l e . l o g ( ke y + ’ = ’ + va l u e )
}
/ / 9 = nine
// 4 = four
// 5 = five
f o r ( l e t [ key , va l u e ] o f myMap . e n t r i e s ( ) ) {
co n s o l e . l o g ( ke y + ’ = ’ + va l u e )
}
/ / 9 = nine
// 4 = four
// 5 = five
myMap . f o r E a c h ( f u n c t i o n ( va l u e , ke y ) {
co n s o l e . l o g ( ke y + ’ = ’ + va l u e ) } )
Iterating through a Map
Elements are iterated in the same order as they were inserted.
(This could be useful, for example for removing duplicates.)
l e t myMap = new Map ( ) ;
myMap . s e t ( 9 , ’ nine ’ ) ;
myMap . s e t ( 4 , ’ four ’ ) ;
myMap . s e t ( 5 , ’ f i ve ’ ) ;
f o r ( l e t ke y o f myMap . ke ys ( ) ) {
co n s o l e . l o g ( ke y )
}
// 9
// 4
// 5
f o r ( l e t va l u e o f myMap . va l u e s ( ) ) {
co n s o l e . l o g ( va l u e )
}
/ / nine
// four
// five
Maps can be constructed from arrays
l e t f i r s t = new Map ( [
[ 1 , ’ one ’ ] ,
[ 2 , ’ two ’ ] ,
[ 3 , ’ three ’ ] ,
]);
co n s o l e . l o g ( merged . g e t ( 1 ) ) ; / / uno
co n s o l e . l o g ( merged . g e t ( 2 ) ) ; / / dos
co n s o l e . l o g ( merged . g e t ( 3 ) ) ; // three
Motivation Revisited
What is the worst-time complexity, if we use a Map:
(1) check whether a word is a valid word, i.e., it appears in a
dictionary;
(2) eliminate all the duplicates in an array of n words
(assuming there are few duplicates);
(3) for all the words in a text, count up how many times each
word appears in the text.
Motivation Revisited
What is the worst-time complexity, if we use a Map:
(1) check whether a word is a valid word, i.e., it appears in a
dictionary;
(2) eliminate all the duplicates in an array of n words
(assuming there are few duplicates);
(3) for all the words in a text, count up how many times each
word appears in the text.
Answers:
(1) constant time O(1): use Map’s method has to check whether
the word is part of the dictionary
(2) linear time O(n): check whether the word exists in dictionary,
add it only when it does not;
(3) linear time O(n): for each word, check whether it is in
dictionary and increment its associated counter value;
Online Programming
Problem: Assume we have a big string, for example representing the content of a large
book, and we want to be able to quickly perform the following task:
TASK: for some arbitrary input words, we want to quickly display the positions at which
they appear in the text (each may appear multiple times);
(2) Build a Map in which the keys are distinct words and the values are an array
containing the positions at which that word appears in the text
Online Programming
Problem: Assume we have a big string, for example representing the content of a large
book, and we want to be able to quickly perform the following task:
TASK: for some arbitrary input words, we want to quickly display the positions at which
they appear in the text (each may appear multiple times);
(2) Build a Map in which the keys are distinct words and the values are an array
containing the positions at which that word appears in the text
(2.1) build an empty Map;
(2.2) fill it by iterating across the elements of arrays (1.3) and (1.4)
(3) Now we can read the arbitrary words requested by the user, and for each, we querry
the dictionary to find the array of positions, which we display.
Summary
We have learned how to use a dictionary (Map)
constant time query (insert,remove,get);
elements can be iterated in insertion order;
useful to get the right complexity in a number of
word-processing tasks.