This project is read-only.
Using System.Collections.Generic.Dictionary

Category:
Arrays, Hash Tables and Dictionaries
Description: Create a histogram of the occurrences of particular unicode characters using a dictionary.
Code:
let SampleHashtbl3() =
let tab = new System.Collections.Generic.Dictionary(30)
let data = "The quick brown fox jumps over the lazy dog"
for i = 0 to data.Length - 1 do
let c = data.Chars(i)
if tab.ContainsKey(c) then
let v = tab.[c]
let _ = tab.Remove(c)
tab.Add(c,v+1)
else
tab.Add(c,1)

for KeyValue(k,v) in tab do
printfn "Number of '%c' characters = %d" k v

//--------------------------------------------------------

Execution Result:
Number of 'T' characters = 1
Number of 'h' characters = 2
Number of 'e' characters = 3
Number of ' ' characters = 8
Number of 'q' characters = 1
Number of 'u' characters = 2
Number of 'i' characters = 1
Number of 'c' characters = 1
Number of 'k' characters = 1
Number of 'b' characters = 1
Number of 'r' characters = 2
Number of 'o' characters = 4
Number of 'w' characters = 1
Number of 'n' characters = 1
Number of 'f' characters = 1
Number of 'x' characters = 1
Number of 'j' characters = 1
Number of 'm' characters = 1
Number of 'p' characters = 1
Number of 's' characters = 1
Number of 'v' characters = 1
Number of 't' characters = 1
Number of 'l' characters = 1
Number of 'a' characters = 1
Number of 'z' characters = 1
Number of 'y' characters = 1
Number of 'd' characters = 1
Number of 'g' characters = 1

Last edited Sep 14, 2011 at 3:46 AM by ttliu2000, version 1

Comments

No comments yet.