Most Frequent Number Following Key In an Array

🏠 ⬅️ ➡️

You are given a 0-indexed integer array nums. You are also given an integer key, which is present in nums.

For every unique integer target in nums, count the number of times target immediately follows an occurrence of key in nums. In other words, count the number of indices i such that:

  • 0 <= i <= nums.length - 2,
  • nums[i] == key and,
  • nums[i + 1] == target.

Return the target with the maximum count. The test cases will be generated such that the target with maximum count is unique.

Example 1:

Input: nums = [1,100,200,1,100], key = 1 Output: 100 Explanation: For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key. No other integers follow an occurrence of key, so we return 100.

Example 2:

Input: nums = [2,2,2,2,3], key = 2 Output: 2 Explanation: For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key. For target = 3, there is only one occurrence at index 4 which follows an occurrence of key. target = 2 has the maximum number of occurrences following an occurrence of key, so we return 2.

Constraints:

  • 2 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000
  • The test cases will be generated such that the answer is unique.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 5
    integer, dimension(n) :: nums = (/ 1, 100, 200, 1, 100 /)
    integer :: key = 1
    integer :: target
    integer :: i
    integer :: count

    do i = 1, n - 1
        if (nums(i) == key .and. nums(i+1) == target) then
            count = count + 1
        end if
    end do

    print '(I0)', target

end program main
Compiled
Executed
Correct
program maxCount
      implicit none
      integer, parameter :: int32 = selected_int_kind(9)

      integer :: i, j, target, key, count, max_count, nums(1000), len

      ! Read the input
      read (*, *) len
      do i = 1, len
        read (*, *) nums(i)
      end do
      read (*, *) key

      ! Initialize the variables
      max_count = 0
      target = 0

      ! Iterate over the array
      do i = 1, len - 1
        ! Check if the current element is equal to the key
        if (nums(i) == key) then
          ! Increment the count
          count = count + 1

          ! Check if the next element is equal to the target
          if (nums(i + 1) == target) then
            ! Increment the count
            count = count + 1
          end if

          ! Check if the count is greater than the maximum count
          if (count > max_count) then
            ! Update the maximum count and the target
            max_count = count
            target = nums(i + 1)
          end if
        end if
      end do

      ! Print the output
      write (*, *) target

      end program maxCount
🌐 Data from online sources
def count_once_in_both(words1, words2):
    counter1 = {}
    counter2 = {}

    for word in words1:
        counter1[word] = counter1.get(word, 0) + 1
    for word in words2:
        counter2[word] = counter2.get(word, 0) + 1

    count = 0
    for item in counter1.items():
        if item[1] == 1 and counter2.get(item[0], 0) == 1:
            count += 1

    return count
  1. Create two hashmaps (counter1 and counter2) to store the frequency of each word in words1 and words2, respectively.
  2. Iterate over the two arrays and increase the word count within their respective hashmap.
  3. Initialize a count variable to 0.
  4. Iterate over the entries in counter1, checking both its value and the value in counter2 for that key (word) against 1. If both are equal to 1, increment the count.
  5. Return the final count.
🌐 Data from online sources
#include <vector>
#include <string>
#include <unordered_map>

int countOnceInBoth(const std::vector<std::string>& words1, const std::vector<std::string>& words2) {
    std::unordered_map<std::string, int> counter1, counter2;

    for (const auto& word : words1)
        counter1[word]++;
    for (const auto& word : words2)
        counter2[word]++;

    int count = 0;
    for (const auto& item : counter1)
        if (item.second == 1 && counter2[item.first] == 1)
            count++;

    return count;
}
  1. Create two hashmaps (counter1 and counter2) to store the frequency of each word in words1 and words2, respectively.
  2. Iterate over the two arrays and increase the word count within their respective hashmap.
  3. Initialize a count variable to 0.
  4. Iterate over the entries in counter1, checking both its value and the value in counter2 for that key (word) against 1. If both are equal to 1, increment the count.
  5. Return the final count.