Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs2-24wi
lectures
Commits
538ff8c1
Commit
538ff8c1
authored
1 year ago
by
Adam Blank
Browse files
Options
Download
Email Patches
Plain Diff
Add new file
parent
15942dd8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
02/MostCommonWord.java
+33
-0
02/MostCommonWord.java
with
33 additions
and
0 deletions
+33
-0
02/MostCommonWord.java
0 → 100644
View file @
538ff8c1
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.util.*
;
public
class
MostCommonWord
{
private
static
final
String
BOOK_FILENAME
=
"alice.txt"
;
public
static
void
main
(
String
[]
args
)
throws
FileNotFoundException
{
// NOTE: This is wrong...do not copy.
// keys are words
// values are number of times that word occurs
Map
<
String
,
Integer
>
map
=
new
HashMap
<>();
Scanner
s
=
new
Scanner
(
new
File
(
BOOK_FILENAME
));
while
(
s
.
hasNext
())
{
String
next
=
s
.
next
();
if
(!
map
.
containsKey
(
next
))
{
map
.
put
(
next
,
0
);
}
int
oldValue
=
map
.
get
(
next
);
map
.
put
(
next
,
oldValue
+
1
);
// map.get(next) = map.get(next) + 1
}
// print size of map
String
best
=
"???"
;
for
(
String
key
:
map
.
keySet
())
{
if
(
map
.
get
(
best
)
==
null
||
map
.
get
(
key
)
>
map
.
get
(
best
))
{
best
=
key
;
}
// ... implement together
}
System
.
out
.
println
(
best
);
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment