how to find repeated values in array?
Show older comments
If we were rolling four 6-sided dice and our array shows that we rolled 2 3 3 2 then the function would count and return that 2 values were repeated (a pair of 2's and a pair of 3's). If we were rolling four 6-sided dice and our array shows that we rolled 2 2 2 2 then the function would count and return that 1 value was repeated (the 2's). Note that in this final example we don't count 2's more than once.
countRepeats([2 3 3 2], 6)
countRepeats([2 2 2 2], 6)
function countRepeats(dieValue,numSides)
countRepeats = hist(dieValue,1:max(dieValue))
end
I tried using this function but it only counts the number of values of each number shown starting from 1. I think I might need a loop for this. Any hints are appriciated.
Answers (1)
Star Strider
on 23 Mar 2021
0 votes
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!