These are the solutions to the exercises found in the section, Exercises . The solutions provided here are in JavaScript, but you can find the solutions in Python and Ruby in the code download. [8]
-
The following implementation first stores the values of the first array in a hash table and then checks each value of the second array against that hash table:
<table><tbody><tr><td> <span> </span></td><td> <strong>function</strong> getIntersection(array1, array2) {</td></tr><tr><td> <span> </span></td><td> <strong>let</strong> intersection = [];</td></tr><tr><td> <span> </span></td><td> <strong>let</strong> hashTable = {};</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>for</strong> ( <strong>let</strong> i = 0; i < array1.length; i++) {</td></tr><tr><td> <span> </span></td><td>hashTable[array1[i]] = <strong>true</strong> ;</td></tr><tr><td> <span> </span></td><td>}</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>for</strong> ( <strong>let</strong> j = 0; j < array2.length; j++) {</td></tr><tr><td> <span> </span></td><td> <strong>if</strong> (hashTable[array2[j]]) {</td></tr><tr><td> <span> </span></td><td>intersection.push(array2[j]);</td></tr><tr><td> <span> </span></td><td>}</td></tr><tr><td> <span> </span></td><td>}</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>return</strong> intersection;</td></tr><tr><td> <span> </span></td><td>}</td></tr></tbody></table>
This algorithm has an efficiency of O(N).
-
The following implementation checks each string in the array. If the string isn’t yet in the hash table, the string gets added. If the string is in the hash table, that means it’s been added before, which means it’s a duplicate! This algorithm has a time complexity of O(N):
<table><tbody><tr><td> <span> </span></td><td> <strong>function</strong> findDuplicate(array) {</td></tr><tr><td> <span> </span></td><td> <strong>let</strong> hashTable = {};</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>for</strong> ( <strong>let</strong> i = 0; i < array.length; i++) {</td></tr><tr><td> <span> </span></td><td> <strong>if</strong> (hashTable[array[i]]) {</td></tr><tr><td> <span> </span></td><td> <strong>return</strong> array[i];</td></tr><tr><td> <span> </span></td><td>} <strong>else</strong> {</td></tr><tr><td> <span> </span></td><td>hashTable[array[i]] = <strong>true</strong> ;</td></tr><tr><td> <span> </span></td><td>}</td></tr><tr><td> <span> </span></td><td>}</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td>}</td></tr></tbody></table>
-
The following implementation begins by creating a hash table out of all the characters we encounter in the string. Next, we iterate over each character of the alphabet and check to see whether the character is contained within our hash table. If it isn’t, it means the character is missing from the string, so we return it:
<table><tbody><tr><td> <span> </span></td><td> <strong>function</strong> findMissingLetter(string) {</td></tr><tr><td> <span> </span></td><td> <em>// Store all encountered letters in hash table:</em> </td></tr><tr><td> <span> </span></td><td> <strong>let</strong> hashTable = {};</td></tr><tr><td> <span> </span></td><td> <strong>for</strong> ( <strong>let</strong> i = 0; i < string.length; i++) {</td></tr><tr><td> <span> </span></td><td>hashTable[string[i]] = <strong>true</strong> ;</td></tr><tr><td> <span> </span></td><td>}</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <em>// Report first unseen letter:</em> </td></tr><tr><td> <span> </span></td><td> <strong>let</strong> alphabet = <em>"abcdefghijklmnopqrstuvwxyz"</em> ;</td></tr><tr><td> <span> </span></td><td> <strong>for</strong> ( <strong>let</strong> i = 0; i < alphabet.length; i++) {</td></tr><tr><td> <span> </span></td><td> <strong>if</strong> (!hashTable[alphabet[i]]) {</td></tr><tr><td> <span> </span></td><td> <strong>return</strong> alphabet[i];</td></tr><tr><td> <span> </span></td><td>}</td></tr><tr><td> <span> </span></td><td>}</td></tr><tr><td> <span> </span></td><td>}</td></tr></tbody></table>
-
The following implementation begins by iterating over each character in the string. If the character does not yet exist in the hash table, the character is added to the hash table as a key with the value of 1, indicating the character has been found once so far. If the character is already in the hash table, we simply increment the value by 1. So, if the character "e" has the value of 3, it means the "e" exists three times within the string.
Then, we iterate over the characters again and return the first character that only exists once within the string. This algorithm is O(N):
<table><tbody><tr><td> <span> </span></td><td> <strong>function</strong> firstNonDuplicate(string) {</td></tr><tr><td> <span> </span></td><td> <strong>let</strong> hashTable = {};</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>for</strong> ( <strong>let</strong> i = 0; i < string.length; i++) {</td></tr><tr><td> <span> </span></td><td> <strong>if</strong> (hashTable[string[i]]) {</td></tr><tr><td> <span> </span></td><td>hashTable[string[i]]++;</td></tr><tr><td> <span> </span></td><td>} <strong>else</strong> {</td></tr><tr><td> <span> </span></td><td>hashTable[string[i]] = 1;</td></tr><tr><td> <span> </span></td><td>}</td></tr><tr><td> <span> </span></td><td>}</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>for</strong> ( <strong>let</strong> j = 0; j < string.length; j++) {</td></tr><tr><td> <span> </span></td><td> <strong>if</strong> (hashTable[string[j]] == 1) {</td></tr><tr><td> <span> </span></td><td> <strong>return</strong> string[j];</td></tr><tr><td> <span> </span></td><td>}</td></tr><tr><td> <span> </span></td><td>}</td></tr><tr><td> <span> </span></td><td>}</td></tr></tbody></table>