📌  相关文章
📜  在给定的数组中找到除 X 之外最频繁的元素 K 位置

📅  最后修改于: 2022-05-13 01:56:17.476000             🧑  作者: Mango

在给定的数组中找到除 X 之外最频繁的元素 K 位置

给定一个数组nums[]和整数KX ,任务是在给定数组中找到远离X的最频繁元素K位置。

例子:

方法:可以使用数组遍历的思想和存储距离 X 为 K 的元素来解决这个问题。

请按照以下步骤解决问题:

  • 在给定数组中搜索所有出现的X
  • 对于X的每次出现,将距离K处的元素及其频率存储在地图中
  • 最后,只需在地图中找到频率最高的元素并将其返回即可。

下面是上述方法的实现。

C++
// C++ code to implement the approach
 
#include 
using namespace std;
 
// Function to find most frequent element
int mostFrequent(vector& nums,
                 int K, int X)
{   
      // Take one map to store count of
    // frequent element
    map m;
    for (int i = 0; i < nums.size() - K;
                                     i++)
        if (nums[i] == X) {
            if (m.find(nums[i + K])
                        != m.end())
                m[nums[i + K]]++;
            else
                m.insert({ nums[i + K], 1 });
            if(i - K >= 0)
                m[nums[i - K]]++;
        }
     
      // Initialize variable ans to store
      // most frequent element
    int ans = 0, count = 0;
    for (auto i : m) {
        if (i.second > count) {
            ans = i.first;
            count = i.second;
        }
    }
 
    // Return ans
    return ans;
}
 
// Driver code
int main()
{
    vector nums = { 1, 100, 200, 1, 100 };
    int K = 1, X = 1;
 
    // Function call
    cout << mostFrequent(nums, K, X);
    return 0;
}


Java
// Java code to implement the approach
import java.io.*;
import java.util.HashMap;
import java.util.Map;
 
class GFG {
 
  // Function to find most frequent element
  static int mostFrequent(int[] nums,
                          int K, int X)
  {
     
    // Take one map to store count of
    // frequent element
    HashMap m = new HashMap();
    for (int i = 0; i < nums.length - K; i++)
      if (nums[i] == X) {
        if (m.containsKey(nums[i + K])){
          m.put(nums[i + K],   m.get(nums[i + K]) + 1 );
        }
        else
          m.put( nums[i + K], 1 );
        if(i - K >= 0){
          if(m.containsKey(nums[i - K]))
            m.put(nums[i - K],  m.get(nums[i - K]) + 1);
        }
      }
 
    // Initialize variable ans to store
    // most frequent element
    int ans = 0, count = 0;
    for (Map.Entry e : m.entrySet()){
      if(e.getValue() > count){
        ans = e.getKey();
        count = e.getValue();
      }
    }
 
    // Return ans
    return ans;
  }
 
  // Driver code
  public static void main (String[] args) {
    int[] nums = { 1, 100, 200, 1, 100 };
    int K = 1, X = 1;
 
    // Function call
    System.out.print(mostFrequent(nums, K, X));
  }
}
 
// This code is contributed by hrithikgarg03188.


C#
// C# code to implement the approach
using System;
using System.Collections.Generic;
 
class GFG {
 
  // Function to find most frequent element
  static int mostFrequent(int[] nums, int K, int X)
  {
     
    // Take one map to store count of
    // frequent element
    Dictionary m = new Dictionary();
    for (int i = 0; i < nums.Length - K; i++)
      if (nums[i] == X) {
        if (m.ContainsKey(nums[i + K]))
          m[nums[i + K]] = m[nums[i + K]] + 1;
        else
          m.Add(nums[i + K], 1);
        if (i - K >= 0
            && m.ContainsKey(nums[i - K])) {
          m[nums[i - K]] = m[nums[i - K]] + 1;
        }
      }
 
    // Initialize variable ans to store
    // most frequent element
    int ans = 0, count = 0;
    foreach(KeyValuePair x in m)
    {
      if (x.Value > count) {
        ans = x.Key;
        count = x.Value;
      }
    }
 
    // Return ans
    return ans;
  }
 
  // Driver code
  public static void Main()
  {
    int[] nums = { 1, 100, 200, 1, 100 };
    int K = 1, X = 1;
 
    // Function call
    Console.Write(mostFrequent(nums, K, X));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
100

时间复杂度: O(N)
辅助空间: O(N)