0

I have to find the number of elements in two char[] arrays. These arrays are char arrays with a max size of 5, and only letters A to G can be used. I will separate this into two arrays - arr1 and arr0. arr1 has priority over arr2, as in if arr1 has 3 elements of 'A' but arr2 has 4, the max count is 3.

I have to find the number of common elements these arrays share, with arr1 having the priority in maximum count. I am stumped so far. I thought of iterating through the chars:

for (char x = 'A'; x <= G; x++) { //code }

But I have no idea what to do after that. Any help would be appreciated.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
Raamiz Ali
  • 23
  • 3

2 Answers2

1

This feels like homework so instead of giving you a full solution I will give you pointers and the parts you need to disassemble / reassemble to make it work, hoping that this will better allow you to learn and understand the suggested solution.

Here's a solution to count elements in a String using HashMap, you can easily adapt it to count elements in an Array:

// Java prorgam to count frequencies of 
// characters in string using Hashmap 
import java.io.*; 
import java.util.*; 
class OccurenceOfCharInString { 
    static void characterCount(String inputString) 
    { 
        // Creating a HashMap containing char 
        // as a key and occurrences as  a value 
        HashMap<Character, Integer> charCountMap 
            = new HashMap<Character, Integer>(); 
  
        // Converting given string to char array 
  
        char[] strArray = inputString.toCharArray(); 
  
        // checking each char of strArray 
        for (char c : strArray) { 
            if (charCountMap.containsKey(c)) { 
  
                // If char is present in charCountMap, 
                // incrementing it's count by 1 
                charCountMap.put(c, charCountMap.get(c) + 1); 
            } 
            else { 
  
                // If char is not present in charCountMap, 
                // putting this char to charCountMap with 1 as it's value 
                charCountMap.put(c, 1); 
            } 
        } 
  
        // Printing the charCountMap 
        for (Map.Entry entry : charCountMap.entrySet()) { 
            System.out.println(entry.getKey() + " " + entry.getValue()); 
        } 
    } 
  
    // Driver Code 
    public static void main(String[] args) 
    { 
        String str = "Ajit"; 
        characterCount(str); 
    } 
} 

And finally you can sort by occurrence and get the highest occurrences, there are plenty of resources about that, here's an Example from StackOverflow:

Map<String, Person> people = new HashMap<>();
Person jim = new Person("Jim", 25);
Person scott = new Person("Scott", 28);
Person anna = new Person("Anna", 23);

people.put(jim.getName(), jim);
people.put(scott.getName(), scott);
people.put(anna.getName(), anna);

// not yet sorted
List<Person> peopleByAge = new ArrayList<>(people.values());

Collections.sort(peopleByAge, Comparator.comparing(Person::getAge));

for (Person p : peopleByAge) {
    System.out.println(p.getName() + "\t" + p.getAge());
}

Source for OccurenceOfCharInString
Source for HashMap sorting

Pitto
  • 8,229
  • 3
  • 42
  • 51
  • Hi there, if you found my code useful please don't forget upvoting it and/or choosing it as final answer. – Pitto Apr 21 '21 at 09:53
0
arr1;
arr2;
int find(char) {
  int a = 0;
  int b = 0;

  for(let i = 0; i < arr1.length; i++) {
    if(arr1[i] === char)
      a++;
  }
  for(let j = 0; j < arr2.length; j++) {
    if(arr2[j] === char)
      b++;
  }
  if(a > 0){
    return a;
  } else {
    return Math.max(a, b);
  }
}

numOccurence = find(char);
Sujio
  • 357
  • 1
  • 2
  • 13