Count of times the current integer has already occurred during Array traversal
Given an array arr[], the task is to find the number of times the current integer has already occurred during array traversal.
Examples:
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
Naive Approach:
The naive approach to solve the problem is to run two nested loops: one to traverse each element of the array and another to traverse from 0 to the i-1 element to check how many times ith element has occurred already.
Algorithm:
1. Create a function that takes an array and its length as input.
2. Traverse the array from 0 to n-1:
a. Initialize a variable count to 0.
b. Traverse the array from 0 to i-1:
i. If the current element matches a previous element, increment count.
c. Print the count for the current element.
Below is the implementation of the approach:
C++
#include<bits/stdc++.h>
using namespace std;
void countOccurences( int arr[], int n) {
for ( int i=0; i<n; i++) {
int count = 0;
for ( int j=0; j<i; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
cout << count << " " ;
}
}
int main() {
int arr[] = { 2, 3, 3, 200, 175,
2, 200, 2, 175, 3 };
int n = sizeof (arr)/ sizeof (arr[0]);
countOccurences(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
public static void countOccurrences( int [] arr, int n) {
for ( int i = 0 ; i < n; i++) {
int count = 0 ;
for ( int j = 0 ; j < i; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
System.out.print(count + " " );
}
}
public static void main(String[] args) {
int [] arr = { 2 , 3 , 3 , 200 , 175 , 2 , 200 , 2 , 175 , 3 };
int n = arr.length;
countOccurrences(arr, n);
}
}
|
Python3
def count_occurrences(arr):
for i in range ( len (arr)):
count = 0
for j in range (i):
if arr[i] = = arr[j]:
count + = 1
print (count, end = " " )
if __name__ = = "__main__" :
arr = [ 2 , 3 , 3 , 200 , 175 , 2 , 200 , 2 , 175 , 3 ]
count_occurrences(arr)
|
C#
using System;
class Program
{
static void CountOccurrences( int [] arr, int n)
{
for ( int i = 0; i < n; i++)
{
int count = 0;
for ( int j = 0; j < i; j++)
{
if (arr[i] == arr[j])
{
count++;
}
}
Console.Write(count + " " );
}
}
static void Main()
{
int [] arr = { 2, 3, 3, 200, 175, 2, 200, 2, 175, 3 };
int n = arr.Length;
CountOccurrences(arr, n);
}
}
|
Javascript
function countOccurrences(arr) {
const n = arr.length;
for (let i = 0; i < n; i++) {
let count = 0;
for (let j = 0; j < i; j++) {
if (arr[i] === arr[j]) {
count++;
}
}
console.log(count + ' ' );
}
}
function main() {
const arr = [2, 3, 3, 200, 175, 2, 200, 2, 175, 3];
countOccurrences(arr);
}
main();
|
Output
0 0 1 0 0 1 1 2 1 2
Time Complexity: O(N*N) as two nested loops are executing. Here, N is size of input array.
Space Complexity: O(1) as no extra space has been used.
Approach: The task can be solved by keeping track of frequencies of distinct elements till the current element using a HashMap. Follow the below steps to solve the problem:
- Create a hashmap say ‘occ‘, to store the frequencies of the distinct elements till the current element
- For the current element, check whether it already exists inside the hashmap or not.
- If it exists, store the frequency corresponding to it, else store 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void getOccurrences(vector< int >& arr)
{
int n = arr.size();
unordered_map< int , int > occ;
for ( int i = 0; i < n; ++i) {
if (occ.find(arr[i]) != occ.end()) {
cout << occ[arr[i]] << " " ;
}
else {
cout << 0 << " " ;
}
occ[arr[i]]++;
}
}
int main()
{
vector< int > arr
= { 2, 3, 3, 200, 175,
2, 200, 2, 175, 3 };
getOccurrences(arr);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void getOccurrences( int [] arr)
{
int n = arr.length;
HashMap<Integer, Integer> occ
= new HashMap<Integer, Integer>();
for ( int i = 0 ; i < n; ++i) {
if (occ.containsKey(arr[i])) {
System.out.print(occ.get(arr[i]) + " " );
occ.put(arr[i], occ.get(arr[i]) + 1 );
}
else {
System.out.print( 0 + " " );
occ.put(arr[i], 1 );
}
}
}
public static void main(String[] args)
{
int [] arr
= { 2 , 3 , 3 , 200 , 175 , 2 , 200 , 2 , 175 , 3 };
getOccurrences(arr);
}
}
|
Python3
def getOccurrences(arr):
n = len (arr)
occ = {}
for i in range ( 0 , n):
if (arr[i] in occ):
print (occ[arr[i]], end = " " )
else :
print ( 0 , end = " " )
occ[arr[i]] = occ[arr[i]] + 1 if arr[i] in occ else 1
if __name__ = = "__main__" :
arr = [ 2 , 3 , 3 , 200 , 175 ,
2 , 200 , 2 , 175 , 3 ]
getOccurrences(arr)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void getOccurrences( int []arr)
{
int n = arr.Length;
Dictionary< int , int > occ =
new Dictionary< int , int >();;
for ( int i = 0; i < n; ++i) {
if (occ.ContainsKey(arr[i])) {
Console.Write(occ[arr[i]] + " " );
occ[arr[i]] = occ[arr[i]] + 1;
}
else {
Console.Write(0 + " " );
occ.Add(arr[i], 1);
}
}
}
public static void Main()
{
int []arr
= { 2, 3, 3, 200, 175,
2, 200, 2, 175, 3 };
getOccurrences(arr);
}
}
|
Javascript
<script>
function getOccurrences(arr) {
let n = arr.length;
let occ = new Map();
for (let i = 0; i < n; ++i) {
if (occ.has(arr[i])) {
document.write(occ.get(arr[i]) + " " );
}
else {
document.write(0 + " " );
}
if (occ.has(arr[i]))
occ.set(arr[i], occ.get(arr[i]) + 1);
else
occ.set(arr[i], 1);
}
}
let arr
= [2, 3, 3, 200, 175,
2, 200, 2, 175, 3];
getOccurrences(arr);
</script>
|
Output
0 0 1 0 0 1 1 2 1 2
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
09 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...