0

i'm currently working on a small project/game that involves guessing words that end in a certain character. in order to win, the player must beat the computer in guessing 3 words that all end in the same letter. For instance, if i choose cookies, snickers, and libraries, i would win because all 3 end in the letter "s."

in my game, each word is processed as it is played and the last character is added to an array list. What is the most efficient method of checking and counting if this letter already exists in the array list? I saw some things on removing duplicates and such, but in my case, I need to be able to identify duplicates and make sure that 3 of the same character exist inside the array list.

2
  • Where are your codes ? And what all you have tried ? Commented May 16, 2016 at 2:42
  • i dont think the code i have so far is that important. The only difficulty i'm having is counting the duplicates in an array list. I honestly have not tried anything for this part of the problem because i'm not sure where to start/what the best method is. I have considered creating an array with 26 slots that i can increment based upon the letter (the letter "a" takes slot 0/25, the letter "c" takes slot 2/25 ect) but i'm not sure what the best way is of determining the characters positioning in the alphabet. edit: i didnt add codes because they wouldn't help me explain my situation Commented May 16, 2016 at 2:44

3 Answers 3

1

As long as it works and produces the expected result, then it's fine. Plus, the solution you describe in your comment can work. That is, create an array of 26 ints and increment it based on the letter.

It is possible to transform a character into an int by casting it. In order to numerate them from 0 to 25, you can do (int) (c - 'a') to get the index. (int) ('a' - 'a') is 0, (int) ('b' - 'a') is 1 etc...

Sign up to request clarification or add additional context in comments.

1 Comment

for some reason when i wrote this post, something wasn't clicking. what i ended up doing was taking a char array "alphabet" (which has a-z as independent characters) and i checked the last letter against the alphabet through a while loop. After this, i took the letters position (0-25) and incremented that position in an integer array to represent which character had more than 1 use. I guess what i was looking for was the ".contains" solution for arrays and counting like this.
0

1) copy the array list 'a' to 'b'

2) sort 'b' (this allows step #3 to be done in one linear pass)

3) iterate through 'b' and keep track of where repeat are.

4) do whatever math you want based on the results

This trades off memory usage for processing speed (plus retaining the original array list as is) (sort is N lg N + 1 N for iteration through list = 2N lg N )

Comments

0

You can use this below code:

ArrayList<Character> characterArrayList;//Remember init arrayList and add elements...
    int[] result = new int[26];
    for (char c : characterArrayList){
        result[c - 'a'] ++;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.