Im looking for an algorithm to reduce a list (playlist) of ordered but not unique items. Searched for set theory but havent found anything suitable yet
Examples
[a, b, b, c] -> [a, b, b, c] Cannot be reduced.
[a, b, b, a, b, b] -> [a, b, b].
[b, b, b, b, b] -> [b].
[b, b, b, b, a] -> [b, b, b, b, a] Cannot be reduced.
Thinking of getting all existing sublists and count each instance. If there is such a sublist where the count times the sublist length is equal to the orginal list, take the shortest sublist matching this criteria.
This seems a bit brute force, there must be a simpler/faster solution available.
For starters, you don't need to check all sublists -- just those with lengths that are factors of the length of the full list.
If your main concern is coding simplicity rather than raw speed, just let a regex engine solve the problem:
/^(.+?)\1+$/
Which is a variant on Abigail's awesome Perl regex to find prime numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With