Home > database >  Returning occurrences of a character in a string
Returning occurrences of a character in a string

Time:06-23

I've got a task where I'm to discover how many times 'A', 'C', 'G' and 'T' occur in a string, and return it in the following format:

A:count C:count G:count T:count

I'm very new to Java having started learning Java 3 days ago I've come across literature referring to a HashMap being the most viable and efficient means of storing and retrieving this data - As such, I opted in this method. I've managed to create the conditionals and store the data, however I'm struggling with the presentation of data as mentioned above.

Apologise for some offensive code in advance, what I have so far is:

public class DNA {
     static void characterCount(String dna) {
        HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();
        char[] dnaArray = dna.toCharArray();
        charCountMap.put('C', 0);
        charCountMap.put('A', 0);
        charCountMap.put('G', 0);
        charCountMap.put('T', 0);
        for (char q : dnaArray) {

            if (q == 'A' || q == 'C' || q == 'G' || q == 'T') {
                charCountMap.put(q, charCountMap.get(q)   1);
            } else {
                continue;
            } 
        }
        System.out.println(charCountMap);
    }

    public static void main(String[] args) {
        characterCount("ACTGSRSSDSGGGHHTYTCCCFDT");
    }
}

I would appreciate any input, advice or signposting to relevant resources for further learning.

Thank you very much for your time!

CodePudding user response:

You just need to add the below code for that formatting instead of System.out.println(charCountMap);:

 for (Entry<Character, Integer> c : charCountMap.entrySet()) {
    System.out.print(c.getKey()  ":"   c.getValue()   " ");
 }

So you will get output like this

A:1 C:4 T:4 G:4

Use "LinkedHashMap" instead of "HashMap" if you want to follow the character sequence.

CodePudding user response:

One way to get the required output is by doing this:

String output = charCountMap.entrySet()
        .stream()
        .sorted(Map.Entry.comparingByKey())
        .map(entry -> entry.getKey()   ":"   entry.getValue())
        .collect(Collectors.joining(" "));
System.out.println(output);

CodePudding user response:

You may concide upper and lower case charachter as well and decide wether "a" is same or different to "A".

If the same, you can change your code to

char[] dnaArray = dna.toUpperCase().toCharArray();

You might also want to make the logic more stable and check, whether the input string is valid

    HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();

    if (dna == null || dna.isEmpty()) {
        return;
    }

    char[] dnaArray = dna.toUpperCase().toCharArray();

Beside this, you could also start cuonting the hole alphabet and use the int value of the corresponding char.

    HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();

    if (dna == null || dna.isEmpty()) {
        return;
    }

    char[] dnaArray = dna.toUpperCase().toCharArray();
    for (char q : dnaArray) {

        for (int charachter = 'A'; charachter <= 'Z';   charachter) {
            if (q == charachter) {
                charCountMap.put(q, charCountMap.getOrDefault(q, 0)   1);
                break;
            }
        }

    }
    System.out.println(charCountMap);

Output:

{A=1, R=1, C=4, S=4, T=4, D=2, F=1, G=4, H=2, Y=1}

EDIT

Use a TreeMap to sort the map entries

TreeMap<Character, Integer> charCountMap = new TreeMap<Character, Integer>();

Output:

{A=1, C=4, D=2, F=1, G=4, H=2, R=1, S=4, T=4, Y=1}
  • Related