【问题标题】:Find the count of elements in two char[] arrays?查找两个 char[] 数组中的元素数?
【发布时间】:2021-04-19 06:25:34
【问题描述】:

我必须找到两个 char[] 数组中的元素数。这些数组是最大大小为 5 的 char 数组,并且只能使用字母 A 到 G。我会将它分成两个数组 - arr1arr0。 arr1 优先于 arr2,如 arr1 有 3 个元素 'A' 而 arr2 有 4 个,则最大计数为 3。

我必须找到这些数组共享的公共元素的数量,其中 arr1 在最大计数中具有优先级。到目前为止我很难过。我想过遍历字符:

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

但我不知道在那之后该怎么做。任何帮助将不胜感激。

【问题讨论】:

  • 要遍历一个数组(arr),我会for (int i = 0; i < arr.length; i++) { ... }

标签: java arrays loops


【解决方案1】:
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);

【讨论】:

    【解决方案2】:

    这感觉像是家庭作业,所以我不会给你一个完整的解决方案,而是给你一些指导以及你需要拆卸/重新组装以使其工作的部件,希望这将更好地让你学习和理解建议的解决方案。

    这里有一个使用 HashMap 对字符串中元素进行计数的解决方案,您可以轻松地将其调整为对数组中的元素进行计数:

    // 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); 
        } 
    } 
    

    最后,您可以按出现次数排序并获得最高出现次数,这方面有很多资源,这里有一个来自 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

    【讨论】:

    • 您好,如果您发现我的代码有用,请不要忘记投票和/或选择它作为最终答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    相关资源
    最近更新 更多