This question already has answers here:
Set vs Array , difference
(3 answers)
Closed 5 years ago.
I have used lots of time arrays in Ruby. But never get a chance to use set. My question is when Set can be useful and when it is better than an array?
From the documentation, the initial definitions go as follows:
Array: An
integer-indexed collection of objects.
Set: A
collection of unordered values with no duplicates.
In a nutshell, you should use Set when you want to make sure that each element in the collection is unique, you want to test if a given element is present in the collection and you won't require random access to the objects.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to re-use a dynamic arrays many times as I consider it a better performance.
Hence, I don't need to create a new dynamic array every time I need it.
I want to ask if it can lead to bugs and inefficiency if I use the same array for several instructions then clear it and reuse it? And how can I correct my procedure, so, it might approach my need.
My code :
procedure Empty(local_array : array of Integer);
var
i : Integer;
begin
for i:= 0 to high(local_array) do
local_array[i]:= nil;
Setlength(local_array, 0);
end;
If you want to reuse your array don't mes with its size. Changing the size of an array or more specifically increasing it is what could lead to the need for data reallocation.
What is array data reallocation?
In Delphi all arrays need to be stored in continuous memory block. This means that if you are trying to increase the size of your array and there already some data after memory block that is currently assigned to your array the whole array needs to be moved to another memory location where there is enough space to store the new array size in one continuous memory block.
So instead of resizing your array leave its size alone and just set value of array items to some default value. Yes this means that such array will still occupy its allocated memory. But that is goal of reusing such array as you avoid overhead for allocating/deallocating memory to your array.
If you go this way don't forget to store your own count of used items in your array since its length may be larger than the number of item actually used.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
What is the difference between Hash tables and random access tables. I feel that they are similar , but wanted to find out the exact differences, Googling did not help me much.
In general Hash Tables are there to be able to map things like various entities to other entities. Depending on programming language it may be mapping tuples to strings, strings to objects, strings to strings and so on - infinite possibilities.
Regular arrays let you address entities using integer index:
array[index] ==> string for example
On the contrary hash maps aka hash tables aka dictionaries aka associative arrays aka hashes etc let you - among other possibilities - map a string to integer for example:
hash_map['Bill'] => 23 etc
For basic understanding go to:
wiki hash tables
Python dicts
PHP arrays
For more advanced understanding I recommend these 2 books:
'Algorithms' by Sadgewick
'Data Structures and Algorithms' by Drozdek
A hash table (aka hash map, or associative array or dictionary or just a hash) is a specific type of random access data structure.
A Hash Table is "random access" because it allows direct, "indexed" access to individual members in constant time. An Array may also be considered a random access data structure, because you can fetch an individual element by its index.
In contrast, a linked list is not a random access data structure, because you need to iterate through its members to find a specific element.
This question already has answers here:
Algorithm for autocomplete?
(9 answers)
Closed 9 years ago.
If a word is typed in Google, it will show a list of words as suggestions in a drop-down list.
For example, if you type what, it will show what is your name, what is your father's name, what is your college name, etc. in 8 words.
What is a suitable data structure, as well as best way to list those suggestions?
I think the best method is to use a trie where each edge is weighted according to the probability that the next letter correspond to this edge so that first suggestions have higher probabilities.
This question already has answers here:
Algebra equation parser for java
(5 answers)
Closed 9 years ago.
My client wants to save an equation formula in a database (Oracle). In this formula they want to use abbreviations of the variables names (field in a table containing the variables) as a descriptive field to see what the formula uses to calculate the result, but wants to be able to calculate the result of the formula when all the variables have values as well.
This means if they change the formula later, the result has to reflect those changes. They have short and long formulas. e.g.
C=(A+B)/100
D=(E+F)/100
G=(3*C)+(4*D)/7
Do you know any reference to something similar to this?
I'm using jsp and Oracle as stated before.
You are on your own. Oracle will not help you much in parsing equations. For simple things, you can iterate over variables and values using SQL REPLACE function and see if that is good enough for you.
I have an assignment to write an algorithm to find duplicate in a Dynamic Sorted Array. I want to write this algoirthm but before starting, I must know the data structure Dynamic Sorted Array but, I dont know it. I tried to googling but I couldn't find anything like Dynamic Sorted Array. would you please guide me? What is this data structure and how dos it look like? thanks.
I think your instructor is simply referring to an array that can change and sort itself, so you can assume that it's always in the correct order and that it is of variable length. If the algorithm is to be written in pseudo-code that's probably all you need to know.
Let's see, you need to understand what a Dynamic Sorted array is:
You already know what a sorted array is, so let's try to understand what a dynamic array is: It's a grow able array where there is no restriction on the size of the array.
So, to summarize, you need to write an array which is:
A. Sorted
B. Dynamic in nature (expanding)
How to implement? Read Dynamic arrays overview and implementation in Java and C++
I assume it means an array whose length is dynamic (i.e. unknown at compile-time), and whose values are sorted.
I never heard of that data structure, but based on the individual words I would guess that it:
Behaves like an array, that is with O(1) access operations get(index) and set(index).
Can be resized if necessary.
Is always sorted.
I don't think though that such a data structure is very efficient for finding duplicates. I would prefer some sort of map, unless you need very simple algorithms.
I would say you may have a typo in your assignment. It might ought to read "sorted Dynamic Array".
However, a dynamic array which always inserts new items in sorted order would probably fit that terminology. So take your dynamic array:
[2][5][7][9]
Inserting the element '8' would result in the following array:
[2][5][7][8][9]