checking if a list contains at least 4 different items

14 views
Skip to first unread message

Sarah Smet

unread,
Jun 20, 2022, 1:41:19 PM6/20/22
to netlogo-users
Hello,

I'd like to check whether a list made of strings contains at least 4 differents items, or not:

for example

set my-list ["A" "B" "C" "A" "A" "B" "D"]
answer is true

set my-list ["A" "B" "C" "A" "A" "B" "B"]
answer is false

is there a command that does that easely ?

Thanks,
Sarah

Charles Staelin

unread,
Jun 20, 2022, 3:23:57 PM6/20/22
to netlogo-users
A combination of remove-duplicates and length should do it.

if (length remove-duplicates my-list) >= 4 [ ... ]

Charles

Jim Lyons

unread,
Jun 20, 2022, 5:37:31 PM6/20/22
to netlogo-users, Sarah Smet
No, but how about a one-liner?

to-report test4 [ #list ]
  report length remove-duplicates #list >= 4
end

to try-it
  print test4 ["A" "B" "C" "A" "A" "B" "D"] 
  ;; answer is true

  print test4 ["A" "B" "C" "A" "A" "B" "B"] 
  ;; answer is false
end

Jim

Sarah Smet

unread,
Jun 21, 2022, 12:50:14 PM6/21/22
to netlogo-users
Thank you Jim and Charles for your answers. I did not know the "remove-duplicate" command and it fits my needs!
Best
Sarah
Reply all
Reply to author
Forward
0 new messages