1

let's say i have some facts like this

something(a,b,[1,2,3,4]).
something(c,b,[2,3]).
something(e,b,[1,3]).
something(b,a,[1,2]).
something(c,a,[3,4]).

now i want to find which element is repeated mostly in list in this case

max_repeated = 3.

thank you for your time and I hope someone will help me

false
  • 10,264
  • 13
  • 101
  • 209
Lumi
  • 15
  • 3

1 Answers1

0
?- pred_key_count([K]>>(something(_,_,L),member(K,L)),T),aggregate(max(V,K),rb_in(K,V,T),max(V,K)).
K = 3,
...

with this definition:

:- meta_predicate pred_key_count(1,-).

pred_key_count(P,T) :-
    rb_empty(T),
    forall( call(P,K), %(something(_,_,L), member(K,L)),
        (   nb_rb_get_node(T,K,N)
        ->  nb_rb_node_value(N,C),
            D is C+1,
            nb_rb_set_node_value(N,D)
        ;   nb_rb_insert(T,K,1)
        )).
CapelliC
  • 59,646
  • 5
  • 47
  • 90