Questions tagged [hash-map]
A data structure that uses a hash function to map identifying values, known as keys, to their associated values
901
questions
56
votes
2
answers
3k
views
Counting all the vowels in a passage of text
My son just started high school and in Math, he was asked to do some stats homework. The exercise was to count all the vowels in a passage of text, then answer some questions about frequency.
Out of ...
44
votes
5
answers
36k
views
Incrementing Integers in Map
I have a HashMap<Token, Integer>, which counts occurrences of Token. Each time Token ...
39
votes
6
answers
64k
views
Java enum containing a hash map for look up
I have the below enum. I need to get the description by code. It is working but can it be improved still?
...
36
votes
1
answer
1k
views
Making a generic NSMapTable replacement written in Swift thread-safe
This is a follow-up to this question.
While discussing some details about the code I posted there, I came upon a problem with thread-safety. After searching and trying different things, I reached a ...
29
votes
5
answers
51k
views
Type-safe Dictionary for various types
Assume the following situation: you have an object that can store any object based on a key (basically, IDictionary<string, object>). You want to store ...
28
votes
8
answers
59k
views
LINQ approach to flatten Dictionary to string
I have a Dictionary<string,string> and want to flatten it out with this pattern:
...
26
votes
2
answers
2k
views
Mapping arbitrary Int values to a linear index space
This is part of a growing "primitive" tools collection here on github, and is the initial use-case for the IntArray for review here.
Often, when processing data, you encounter unique values that you ...
19
votes
5
answers
63k
views
Get value from dictionary given a list of nested keys
I would like to get a deeply-nested value, for example {"a":{"b":{"c":"myValue"}} by providing the keys to traverse. I tried chaining together .get() but that didn'...
19
votes
3
answers
74k
views
Java implementation of spell-checking algorithm
This little program was written for an assignment in a data structures and algorithms class. I'll just note the basic requirements:
Given an inputted string, the program should check to see if it ...
18
votes
1
answer
34k
views
Adjacency List Graph representation on python
I began to have my Graph Theory classes on university, and when it comes to representation, the adjacency matrix and adjacency list are the ones that we need to use for our homework and such. At the ...
17
votes
1
answer
619
views
Python Dictionary Black Magic
I am defining a subclass of a the python dictionary object mpCmd where every item is converted to a lambda. The intended usage is that every item in the dictionary ...
16
votes
3
answers
73k
views
Set a default if key not in a dictionary, *or* value is None [closed]
Let's say I have a key 'messages' that is usually a list, but might be None, or might not be present. So these are all valid inputs:
...
15
votes
1
answer
379
views
Persisted Dictionary Implementation
I'm using this code in an app that has hundreds of daily users. The code works, and passes all of the test cases I have.
Occasionally some users report a problem that I don't get right to the bottom ...
14
votes
3
answers
3k
views
Count word frequencies, and print them most-frequent first
I'm writing a comparison of a simple word-counting program in various languages. The task is to count the frequencies of unique, space-separated words in the input, and output the results most ...
14
votes
4
answers
15k
views
Using a Dictionary to retrieve BOOL values
I have some values pulled from a web service giving me a bool value for a key. I did it this way, but feel like it is repeating without need. How can I refactor this?
Please note that the key on ...
14
votes
5
answers
27k
views
Python Hash Table Implementation
I'm attempting to implement a basic hash table in Python using only lists. Any tips would be appreciated (including better hash functions).
I intend this to handle collisions with separate chaining.
...
14
votes
3
answers
5k
views
Create dictionary with default immutable keys
I've created a dictionary subclass with a set of default immutable keys.
...
13
votes
3
answers
4k
views
Codewars: Reduce strings to one character
I am trying to solve a puzzle in a performant way. This is the puzzle.
Simply explained: you have a String and you must reduce it to one character with the given rules. This is the solution to the ...
13
votes
2
answers
32k
views
HashTable implementation
I have been trying to implement the Hash Table function in Java and here is what I came up with. I would just like to get an opinion on this work. Would there be a better way or any improvement to ...
13
votes
2
answers
6k
views
Count the occurrence of each unique word in the file
I've been doing a task for an interview that I will have soon.
The requirement is to code the following problem in C#.
Write a program (and prove that it works) that: Given a text file, count the ...
13
votes
2
answers
5k
views
Hashtable Implementation - C
HashTable implementation
The Specifics:
It uses a simple hash function, it recieves a string and adds the ASCII values of each caracter, this can cause problems if the string is big, but for this ...
13
votes
2
answers
31k
views
Perfect Hashing Implementation
I'm building a hash table for this problem:
Implement a class for storing a set of integers:
...
13
votes
1
answer
6k
views
Simple trie implementation in JavaScript
After experimenting with a sorted trie implementation in C, I felt that I understood tries pretty well, but was having trouble explaining how they work. Since the C code was based on existing code, I ...
12
votes
4
answers
6k
views
Extraction of value from a dict-tree, given a path of arbitrary length
Problem: We read a complex json-file which grants us a tree in terms of a python "dict of dicts". We're then requested to retrieve a specific path from the tree, represented by a list of elements. The ...
12
votes
3
answers
3k
views
Determine if one string is a permutation of the another
Can anyone give me some feedback if I'm using std::unordered_multimap optimally? I don't know the ins-and-outs of the ...
12
votes
3
answers
2k
views
Implementing a rectangle class
First I've to indicate that this is my homework.
An example of a given input is:
s1 = {'p1': (x1, y1), 'p2': (x2, y2)}
where all names and coordinations are ...
12
votes
3
answers
12k
views
Creating zip but for dictionaries
I found myself needed zip but for dictionaries, and wanted to know if there is any way I can make this shorter and more concise, and/or faster. Also I cannot figure out what the type hinting might be ...
12
votes
6
answers
64k
views
Adding to a dictionary and skipping duplicate keys
What is the cleanest way to use dictionaries and protect against an unhandled ArgumentException if the key already exists in the dictionary?
I'd like to say that I ...
12
votes
3
answers
9k
views
HashTable implementation in C++
I made a simple hash table and I was wondering if there was any way to increase the efficiency of search times. My table class is called Dictionary, because C# has ...
12
votes
3
answers
15k
views
Simple C# HashTable Implementation
Haven't put one of these together since college. How does this look overall?
...
12
votes
3
answers
706
views
Key Order Agnostic, 2D Dictionary
Note:
I am using Unity3D game engine, which uses .Net 2.0 (.Net 2.0.50727.1433 to be precise) to compile code, so I don't have access to newest features of C# 6, or even C# 5. Please keep this in ...
12
votes
3
answers
12k
views
Hash map implementation
I'm a student in college, and while I have coded a lot, I still feel like I've only scraped the surface. I have learned a lot of things over the years, but very few of my professors ever did code ...
12
votes
2
answers
38k
views
Object key-value map reversal
I have implemented functions that will let me reverse a one-to-many value 'Map', that is a plain JavaScript object.
For example,
...
12
votes
1
answer
2k
views
Enumerable Custom Collections in VBA with Dictionary features like Exists
To Collect or Hash
The VBA.Collection has a number of limitations, but it is enumerable and you can refer to items by index or key. But the VBA implementation of a ...
11
votes
3
answers
4k
views
Removing entries from a dictionary containing bad words
I have a dictionary with each item containing a quote in another dictionary:
...
11
votes
10
answers
3k
views
Writing an anagram efficiently
I know this question has been asked before now but in Python. Recently, I set out to write an anagram in C#. For instance, orchestra can be rearranged into carthorse and the anagram should not have ...
11
votes
6
answers
18k
views
"Multi-key" dictionary
I was making a relatively large-scale project in Python as a learning experiment, and in the process I found that it would be useful to have a map (or, in Python, I guess they're called dictionaries) ...
11
votes
4
answers
18k
views
HashMap implementation in Python
Here's my implementation of a bare-bones HashMap in Python.
Expected behavior, it returns None if a key is missing,
When a key is inserted the second time it just overwrites the first value.
...
11
votes
4
answers
54k
views
Dictionary GetValueOrDefault
I'm pretty sure there's no better way to do this but I thought a consensus on here might be nice. Let me know what you think.
...
11
votes
3
answers
330
views
Conditionally set attributes in MongoDB based on form fields
I have code which gets data from a POST request (request.form dict), compares with corresponding attribute in object ...
11
votes
2
answers
5k
views
Invert a dictionary to a dictionary of lists
I have a dictionary whose keys are related to list of values, but any value may be related to many keys, hence be contained in various lists-attached-keys.
To highlight such relation; I build (in ...
11
votes
6
answers
18k
views
Enhanced Hashmap - Add a number to all keys/values
I had the below problem in a coding test and I got 28/30 tests passes and 2 failed due to a time-out.
Problem You have created a programming language and now you have decided to add ...
11
votes
2
answers
18k
views
Convert a list of dictionaries to a dictionary of dictionaries
I need to convert a list of dictionaries to a dictionary of dictionaries, with a particular item as the new key.
This is the way I'm currently doing it:
...
11
votes
3
answers
3k
views
Recursive Insertion in Hash Table (Linear Probing)
I was practicing implementing a hash function. I tried to use linear probing to insert in hash table. The insertion is working alright but I am not satisfied with the recursion. It doesn't feel like "...
11
votes
2
answers
4k
views
Extending CGPoint to conform to Hashable
For Advent of Code Day 1, I found myself needing to use a tuple as a key in a dictionary. Seeing that I can't extend tuples in Swift, I decided to extend CGPoint ...
11
votes
3
answers
2k
views
Find the common ancestor between two nodes of a tree
Here is my code in Python for to find a common ancestor between two nodes in a particular tree. This is a question from Cracking the Coding Interview that I decided to implement on my own. No one has ...
11
votes
2
answers
26k
views
Mapping enum to enum
I need to map from one enum type to another. Here is the enum I am
given:
...
11
votes
2
answers
1k
views
Finding roughly matching genome sequences in Python dictionary
The purpose of my code here is to play a part in genome sequencing analysis, and while functional it takes days to run, so I am looking for any way I can improve speed. The input is up to 500 million ...
11
votes
2
answers
24k
views
Hash table implementation in Java
For class, I was asked to write a (linear probing) hash table in Java. (I was also asked to write a linear-chaining hash table, which is why I named this one ...
11
votes
3
answers
41k
views
Map with different types for values
I am trying to implements a map (that can work as a cache) that has strings as keys and values of different types. In my real case once set the key/values in the map will not change.
I used these ...