[C++?] Counting the maximum number of equal objects in a vector

Click For Summary

Discussion Overview

The discussion revolves around finding an efficient algorithm to count the maximum number of equal objects in a vector as new objects are added. The scope includes algorithmic efficiency and implementation strategies in C++.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests continuous sorting of the vector after defining a comparison relation, estimating a worst-case time complexity of O(n^2).
  • Another participant proposes using a hashmap or dictionary to track the count of each object as it is added, incrementing the count if the object already exists.
  • An alternative method is mentioned, where the object's value could be used as an index in a large array, with the overhead of initializing and scanning the array noted.
  • A participant expresses concern about the time required for converting objects into long integers for indexing, questioning the efficiency of this conversion in C++.
  • Further discussion highlights that using a hashmap is more space-efficient compared to using a large array, especially considering potential collisions in the hash table.
  • Assembly code is suggested for fast searching of an array, with specific instructions for different bit values provided.

Areas of Agreement / Disagreement

Participants present multiple competing views on the best approach to count equal objects, with no consensus reached on a single method being superior.

Contextual Notes

Participants mention potential limitations regarding memory usage and the efficiency of different data structures, but these aspects remain unresolved.

Who May Find This Useful

Readers interested in algorithm design, C++ programming, and data structure optimization may find this discussion relevant.

burritoloco
Messages
81
Reaction score
0
Hi, say I have a vector that is currently being populated with some objects. I would like, as each object is added to the vector, to count the maximum number of equal objects currently in the vector. Would you know of any efficient algorithm that does this? Thank you.
 
Technology news on Phys.org


I'm guessing that perhaps continuous sorting (after defining a comparison relation on the objects) as the objects are added, and then counting the equalities could be a way to do this. Probably would take about O(n^2) at worst (n is the total number of objects to be added). Is there another way?
 


Use a hashmap\dictionary. As you add the item to the vector check the dictionary and if the item exists increment the value otherwise add it with a value of 1.
 
Last edited:


As an alternative to a hash algorithm, depending on the number of bits in each object, you could use the object's value as an index into a very large array, and increment the array for each object. The overhead would be the time it takes to zero out the array to initialize it, and when done, to scan the array for non-zero values.
 
Thanks guys. The hashmap idea looks straightforward. rcgldr, I can define a function to convert these objects into longs (to be used as indices of an array as you suggested), but this would additional time, no? Is there a native c++ way to do this conversion? (the usual type casting doesn't work here).
 
burritoloco said:
The hashmap idea looks straightforward. rcgldr, I can define a function to convert these objects into longs (to be used as indices of an array as you suggested), but this would additional time?
Yes, and assuming you mean 32 bit longs, it would take too much memory, the array size would be 4 billion elements. Using values as indexes is a software alternative to a content addressable (also called associative) memory. A hashmap does the same thing but uses much less space (assuming only a small sub-set of possible values are used). The hash table will have "collisions", and generally you'd want the hash table to end up about 1/2 empty to reduce collision overhead.

You could use assembly code to do a fast search of an array for a value, using repne scas... for 8 (scasb), 16 (scasw), 32 (scasd), or 64 (scasq) bit values, either inline or as a separate module.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 30 ·
2
Replies
30
Views
5K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
Replies
13
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K