Any ideas how to retrieve the maximally repeated element in a list.
i.e. something like below,
?- maxRepeated([1,2,7,3,6,1,2,2,3],M).
M = 2.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This solution sorts the list, granting elements to appear sequentially — there’s no need to maintain all elements, once they’re not repeating later.
Your prolog interpreter must have the function
msort(), which sorts a list maintaining duplicated entries.The complexity is given by the sort used, usually
O(n log n), once, after the sort, the list is traversed only once, aggregating the elements and keeping track of the most frequent one.Regards!