count duplicate items in a list.

Hi
Sorry if this has been asked before i searched high and low and came up with nothing!!

I Have a list with duplicate items inside, and i need to count how may there are.

set the_list to {"steve","steve","steve","luke","luke","zach","lindsey"}

in the above list you can see that steve is in there 3 times and luke is in there twice. all i want to be able to do is count them
ignoring the single items.
The number i’m looking for is 5

But i just can’t seem to get my head round what i need to do.
i would be very grateful for any help you guys can give me with this!!
I think i might need to get back to the basics with my scripting, i don’t get chance to do it as much and think i might have lost a little bit
of what little knowledge i had!! :frowning:

Hi pidge,

try this, it parses the list by removing an item one by one and comparing it with the remaining items


set the_list to {"steve", "steve", "steve", "luke", "luke", "zach", "lindsey"}

set c to count the_list
set cnt to 0
repeat with i from 1 to c
	if i is 1 then
		set tempList to rest of the_list
	else if i is c then
		set tempList to items 1 thru -2 of the_list
	else
		tell the_list to set tempList to items 1 thru (i - 1) & items (i + 1) thru -1
	end if
	if item i of the_list is in tempList then set cnt to cnt + 1
end repeat
cnt --> 5


Hi Stefan

Fanastic! :wink:

I just couldn’t seem to get there with anything i tried,
and looking at yours i was a good bus ride away from getting it.
Your script worked perfectly thank you!!

Cheers

Here’s a development of Stefan’s idea:

set the_list to {"steve", "steve", "steve", "luke", "luke", "zach", "lindsey"}

set cnt to 0
repeat with i from 1 to (count the_list)
	set v to item i of the_list
	set item i of the_list to {v} -- {v} is guaranteed not equal to v.
	if (v is in the_list) then set cnt to cnt + 1
	set item i of the_list to v
end repeat
cnt --> 5

Hi Nigel

That works like a charm for me also.

Thanks again to both Stefan and yourself.
i so need to get back into scripting!

Cheers