Sort combination of data based on maximum of occurrence

Sort combination of data based on maximum of occurrence



I have a combination of data of RGBA values in a memory table I got from Rest Response Adapter like these for each row:


"R", "A"
"B"
"A","R"
"R","G"
"B"
...



I need to sort these data based on the maximum of occurrence or repeat inside the table. For example values "R","A" = "A","Rthis mean the count of this value is 2 and so on.


"R","A" = "A","R



I was thinking to use Dictionary but it does not have a method to count values and consider RA = AR. And if I loop for each value it will take very long time so how I should solve this problem ?





But you can swap a pair of values before adding into dictionary (sort for long sequences).
– MBo
Aug 31 at 19:08





@MBo How to swap ? and RGBA is not fixed it can be later RGBO.
– Wel
Aug 31 at 19:14





So, count them, and then sort them.
– David Heffernan
Aug 31 at 19:35





How to swap? 'RA'=>'AR'. An I feel that you have not described all the important details of ypur problem.
– MBo
Aug 31 at 19:48



How to swap?




1 Answer
1



It seems that you have something highly specific. In this case you can optimize it using bit operations:


type
TCounts = array[0..15] of Integer;

const
RGBA_R = 1;
RGBA_G = 2;
RGBA_B = 4;
RGBA_A = 8;

function GetCounts(const Strings: array of string): TCounts;
var
i, j, k: Integer;
s: string;
begin
Fillchar(Result, Sizeof(Result), 0);
for i := 0 to Length(Strings) - 1 do
begin
k := 0;
s := Strings[i];
for j := 1 to Length(s) do
case s[i] of
'R': k := k or RGBA_R;
'G': k := k or RGBA_G;
'B': k := k or RGBA_B;
'A': k := k or RGBA_A;
'"', ' ', ',': ; // do nothing
else
// handle error here
end;

Inc(Result[k]);
end;
end;



The total number of all combinations of R, G, B is Result[RGBA_R or RGBA_G + RGBA_B].


R, G, B


Result[RGBA_R or RGBA_G + RGBA_B]



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)