Just have the program look at the last and lowest note. That's probably it. And analyze the chord, major or minor...
I like this question because it captures a misunderstanding I had, actually one that I still struggle with: what is the purpose of having a key!! In the old days (i.e, 2-3 years ago before I found this website), I thought they the purpose of key marking was simply a way to minimize the number of accidentals drawn on the page. So then the plymypianodotcom's question is easy: You put A#, C#, D#, F# and G# at the beginning of the sheet of music and you are done. (Or do the same with flats if you care).But now I'm starting to understand that if you have a "key" then you have certain chords in that key that will sound "recognizable" to ears that have been listening to western classical music. And conversely that the pianist should be bringing out in the music <this bit I find hard to put in words>. So the way plymypianodotcom computer program should be heading is not counting keys, but counting chord patterns. And by chords that means arpeggios too.
Say I know the following information about how many times each type of key is played in a song. How can I figure out what key signature the song is in? (I am writing a computer program to calculate it). Is it possible to tell if the key is major or minor? How accurate overall would this be? For example I have a song with:A: 80 (This key was pressed 80 times throughout the song)A#/Bb: 406 (This key was pressed 406 times throughout the song)B: 66 (This key was pressed 66 times throughout the song)C: 208 (This key was pressed 205 times throughout the song)C#/Db: 458 (This key was pressed 458 times throughout the song)D: 6 (This key was pressed 6 times throughout the song)D#/Eb: 432 (This key was pressed 432 times throughout the song)E: 94 (This key was pressed 94 times throughout the song)F: 312 (This key was pressed 312 times throughout the song)F#/Gb: 444 (This key was pressed 444 times throughout the song)G: 12 (This key was pressed 12 times throughout the song)G#/Ab: 468 (This key was pressed 468 times throughout the song)Thanks for any help you can provide!