在数组遍历期间当前整数已经出现的次数
给定一个数组arr[] ,任务是找出当前整数在数组遍历期间已经出现的次数。
例子:
Input: arr[] = {2, 3, 3, 200, 175, 2, 200, 2, 175, 3}
Output: 0 0 1 0 0 1 1 2 1 2
Explanation: Before the 0th index, 2 is encountered 0 times. Before the 2nd index, 3 is encountered once at index 1. Similarly, before the 7th index, 2 is occurred twice and so on.
Input: arr[] = {200, 200, 55, 200, 55, 2, 3, 2}
Output: 0 1 0 2 1 0 0 1
方法:可以通过使用 HashMap 跟踪不同元素的频率直到当前元素来解决该任务。请按照以下步骤解决问题:
- 创建一个 hashmap 说' occ ',以存储不同元素的频率直到当前元素
- 对于当前元素,检查它是否已经存在于hashmap中。
- 如果存在,则存储其对应的频率,否则存储0 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of
// times current integer has already
// occurred during array traversal.
void getOccurrences(vector& arr)
{
// Store the size of array
int n = arr.size();
// Hashmap to keep track of
// the frequencies
unordered_map occ;
for (int i = 0; i < n; ++i) {
// If element is already
// present inside hashmap
if (occ.find(arr[i]) != occ.end()) {
cout << occ[arr[i]] << " ";
}
else {
cout << 0 << " ";
}
// Increment the frequency
occ[arr[i]]++;
}
}
// Driver Code
int main()
{
vector arr
= { 2, 3, 3, 200, 175,
2, 200, 2, 175, 3 };
getOccurrences(arr);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the number of
// times current integer has already
// occurred during array traversal.
static void getOccurrences(int[] arr)
{
// Store the size of array
int n = arr.length;
// Hashmap to keep track of
// the frequencies
HashMap occ
= new HashMap();
for (int i = 0; i < n; ++i) {
// If element is already
// present inside hashmap
if (occ.containsKey(arr[i])) {
System.out.print(occ.get(arr[i]) + " ");
// Increment the frequency
occ.put(arr[i], occ.get(arr[i]) + 1);
}
else {
System.out.print(0 + " ");
// Increment the frequency
occ.put(arr[i], 1);
}
}
}
// Driver Code
public static void main(String[] args)
{
int[] arr
= { 2, 3, 3, 200, 175, 2, 200, 2, 175, 3 };
getOccurrences(arr);
}
}
// This code is contributed by ukasp.
Python3
# python3 program for the above approach
# Function to find the number of
# times current integer has already
# occurred during array traversal.
def getOccurrences(arr):
# Store the size of array
n = len(arr)
# Hashmap to keep track of
# the frequencies
occ = {}
for i in range(0, n):
# If element is already
# present inside hashmap
if (arr[i] in occ):
print(occ[arr[i]], end=" ")
else:
print(0, end=" ")
# Increment the frequency
occ[arr[i]] = occ[arr[i]] + 1 if arr[i] in occ else 1
# Driver Code
if __name__ == "__main__":
arr = [2, 3, 3, 200, 175,
2, 200, 2, 175, 3]
getOccurrences(arr)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the number of
// times current integer has already
// occurred during array traversal.
static void getOccurrences(int []arr)
{
// Store the size of array
int n = arr.Length;
// Hashmap to keep track of
// the frequencies
Dictionary occ =
new Dictionary();;
for (int i = 0; i < n; ++i) {
// If element is already
// present inside hashmap
if (occ.ContainsKey(arr[i])) {
Console.Write(occ[arr[i]] + " ");
// Increment the frequency
occ[arr[i]] = occ[arr[i]] + 1;
}
else {
Console.Write(0 + " ");
// Increment the frequency
occ.Add(arr[i], 1);
}
}
}
// Driver Code
public static void Main()
{
int []arr
= { 2, 3, 3, 200, 175,
2, 200, 2, 175, 3 };
getOccurrences(arr);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
0 0 1 0 0 1 1 2 1 2
时间复杂度:O(N)
辅助空间:O(N)