Is it possible to develop a C equivalent of the typescript Record type?

Is there a way to create a record type equivalent in C like what can be done in TypeScript, and add attributes during runtime?

I am aiming to replicate the following:

const familyData: string[] = ["paul", "em", "matthias", "kevin"];
const myFamily: Record<string, boolean> = {};

familyData.forEach(d => myFamily[d] = true);

Why would I want to do this in C? Well, if I could achieve this, I would be able to quickly determine if Kevin exists or if someone like Gregory doesn't by simply doing this:

const name: string = "kevin"; 
console.log(familyData["kevin"]); 

This approach would save me time when performing calculations.

It's worth noting that in the future, I plan to not only associate a name with a true value but also some additional data.

Currently in C, I am stuck iterating through the entire array to find a person's name, causing the time taken to vary depending on the array size each time I need to access data.

Does anyone have a solution for creating something similar to this in C?

Thank you very much.

Answer №1

In order to convert my response into a solution:

You need a data structure that can associate strings with values. One option is to use a hash table with string keys.

Since you're only dealing with true or false values, a hash set could also work; the absence of a person in the set could be considered as a false value.

As Craig Estey mentioned in the comments, red-black trees or tries could be alternatives for this specific case.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensure that Angular waits for the subscription to be completed before triggering the function to generate an Excel

I've encountered an issue with a function that generates an excel from an API request on my webpage. There's a download button that triggers the downloadCSV() function, which usually works fine except when I click it too quickly while navigating ...

What is the method for substituting one text with another using two-way data binding?

I implemented two different cases in my Mat-Table. When there is no data, the user will see a message saying "No Data Found". However, if the user enters text in the filter search, the "No Data Found" message should be hidden and replaced with the entered ...

Express not functioning properly with custom error handler

I'm facing an issue while trying to implement a custom error handler for my Express routes. Despite following the instructions in the documentation which recommend placing the custom handler at the end of the use chain, it seems that the default error ...

Combining two arrays with varying lengths based on their values

Seeking assistance with a programming task that is straightforward yet challenging for me. There are two arrays: one long and one short. var arrayShort = [ { id: 'A', name: 'first' },{ id: 'B', name: &ap ...

Launch a TypeScript Node.js server on Heroku deployment platform

I'm having trouble deploying a Node.js server built with TypeScript on Heroku. Despite following various tutorials and searching through Stack Overflow for solutions, I can't seem to make it work. Here is the code I have in my tsconfig.json and p ...

Display array elements in a PDF document using pdfmake

Upon reaching the final page of my Angular project, I have an array filled with data retrieved from a database. How can I utilize pdfmake to import this data into a PDF file? My goal is to display a table where the first column shows interv.code and the ...

MySQL's `trader_sma` function retrieves an array suitable for traders

Although I'm not a professional programmer, I do have some knowledge of PHP and can write simple scripts. One script I created downloads prices of all coins from an exchange every minute (using a cron job) to store in my database. Now, I'm lookin ...

What is the proper way to utilize a service within a parent component?

I need assistance with setting up inheritance between Child and Parent components. I am looking to utilize a service in the Parent component, but I have encountered an issue. When attempting to input the service in the Parent constructor like this: expor ...

Unique Behavior of SQL and PHP (When Only 1 Row is Returned)

Experiencing a little hiccup with my PHP / SQL coding. Here's what I have: Code: $query = "SELECT DISTINCT student FROM classes LIMIT 100"; $result = mysqli_query($link, $query); $row = mysqli_fetch_array($result, MYSQLI_NUM); print_r($row); After ...

Struct object not found within nested array during JSON unmarshaling

Encountered an issue with unmarshalling a string back to a struct object that contains a nested array of struct objects. The problem is demonstrated in the following code snippet: The JSON string is as follows: const myStr = `{ "name": "test_session1", ...

Ensure that selecting one checkbox does not automatically select the entire group of checkboxes

Here is the code I'm using to populate a list of checkboxes. <label class="checkbox-inline" ng-repeat="item in vm.ItemList track by item.id"> <input type="checkbox" name="item_{{item.id}}" ng-value="{{item.id}}" ng-model="vm.selectedItem" /& ...

Is it possible to set up TypeScript npm packages to be installed in their original TypeScript format rather than JavaScript for the purpose of examining the source code?

Despite my lack of expertise in the inner workings of how a TypeScript library compiles itself to JavaScript before being placed in the node_modules directory, I have a question: Coming from a PHP background, I am accustomed to being able to explore any l ...

Unable to find solutions for all parameters in AnalysisComponent: ([object Object], ?, ?, [

As a newcomer to the Angular framework, I encountered an issue when adding project services. Error: Can't resolve all parameters for AnalysisComponent: ([object Object], ?, ?, [object Object], [object Object], [object Object], [object Object], [obj ...

Passing a method as a parameter type

As I delve into learning JavaScript from Objective-C, I find myself pondering whether it is possible to have a method with parameter types in Objective-C. Let's take the example of the findIndex() JavaScript function that identifies and returns the in ...

Optimal approach for incorporating individual identifiers into a complex hierarchy of object arrays

I am looking to assign unique ids to an array of objects: For example: const exercises = [ { type: "yoga", locations: [ { name: 'studio', day: 'Wednesday' }, { name: 'home' ...

Is it possible to consolidate this type definition?

I generated the following code snippet: type NumberFields<T, K extends keyof T> = T[K] extends number ? K : never; type AnsFields<T> = SomeOtherList & NumberFields<T, keyof T>; In the code above, SomeOtherList consists of predefined ...

What is the best way to specify a function type that takes an argument of type T and returns void?

I am in search of a way to create a type that can accept any (x: T) => void function: let a: MyType; a = (x: number) => {}; // (x: number) => void a = (x: string) => {}; // (x: string) => void a = (x: SomeInterface) => {}; / ...

Developing interconnected dropdowns in Angular 8 for input fields

Imagine we have a list of names structured like this: nameSelected: string; names: Name[ {firstName: 'John', middleName: 'Danny', lastName: 'Smith'}, {firstName: 'Bob', middleName: 'Chris', lastN ...

Encountered issue with accessing the Error Object in the Error Handling Middleware

Below is the custom error validation code that I have developed : .custom(async (username) => { const user = await UserModel.findOne({ username }) if (user) throw new ConflictError('username already used& ...

Can a type be established that references a type parameter from a different line?

Exploring the Promise type with an illustration: interface Promise<T> { then<TResult1 = T, TResult2 = never>( onfulfilled?: | ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ...