using namespace std;
int left(int arr[],int n){
int l=0;
int h=n-1;
int mid;
int val=2;
while(lval)
h=mid;
else if(arr[mid]<val)
l=mid;
else if(arr[mid]==val&&arr[mid-1]<val){
return mid;
}
else if(arr[mid]==val&&arr[mid-1]==val){
h=mid;
}
}
}
int right(int arr[],int n){
int l=0;
int h=n-1;
int mid;
int val=2;
while(lval)
h=mid;
else if(arr[mid]val){
return mid;
}
else if(arr[mid]==val&&arr[mid+1]==val){
l=mid;
}
}
}
int main()
{
int arr[]={ 1, 2, 2, 2, 2, 3, 4, 7, 8, 8 };
int n=sizeof(arr)/sizeof(arr[0]);
int l=left(arr,n);
int r=right(arr,n);
cout<<r-l+1;
#include
int main()
{
int n, a[100], count=0,i, num;
printf(“Enter a number\n”);
scanf(“%d”,&num);
printf(“enter the size of the array\n”);
scanf(“%d”,&n);
printf(“enter the array elements\n”);
for(i=0; i<n;i++)
{
scanf("%d",&a[i]);
}
def countocurrences(arr,n,x):
count = 0
for i in range(len(A)):
if A[i] ==x:
count+=1
return count
arr = [1,2,2,2,2,3,3,2,2,2,2]
x = 2
n = len(arr)
print(countocurrences(arr,n,x))
#include
void main()
{
int n,i,key,count=0;
scanf(“%d”,&n);
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
key=3;
for(i=0;i<n;i++)
{
if(key==arr[i])
{
count++;
}
}
if(count==0)
{
printf("-1");
}
else
{
printf("%d",count);
}
}
#include
using namespace std;
int left(int arr[],int n){
int l=0;
int h=n-1;
int mid;
int val=2;
while(lval)
h=mid;
else if(arr[mid]<val)
l=mid;
else if(arr[mid]==val&&arr[mid-1]<val){
return mid;
}
else if(arr[mid]==val&&arr[mid-1]==val){
h=mid;
}
}
}
int right(int arr[],int n){
int l=0;
int h=n-1;
int mid;
int val=2;
while(lval)
h=mid;
else if(arr[mid]val){
return mid;
}
else if(arr[mid]==val&&arr[mid+1]==val){
l=mid;
}
}
}
int main()
{
int arr[]={ 1, 2, 2, 2, 2, 3, 4, 7, 8, 8 };
int n=sizeof(arr)/sizeof(arr[0]);
int l=left(arr,n);
int r=right(arr,n);
cout<<r-l+1;
return 0;
}
#include
#include
int main()
{
int arr[] = {1, 1, 2, 2, 2, 2, 3,},x;
scanf(“%d”,&x);
int i,count=0;
for(i=0;i<(sizeof(arr)/sizeof(int));i++)
{
if(arr[i]==x)
count++;
}
printf("%d",count);
return 0;
}
n=list(input().split(” “))
m=input()
c=n.count(m)
if c>0:
print(c)
else:
print(-1)
a=[]
for i in range(5):
a.append(int(input()))
b=int(input(“\nEnter x: “))
c=a.count(b)
print(c)
import math
num=[1,2,3,5,5,5,6,7]
def binarysearch(num,left,right,target):
mid=math.floor((left+right)/2)
while(lefttarget:
right=mid-1
else:
left=mid+1
return -1
def occurance(num):
mid=(0+len(num)-1)//2
target=num[mid]
firstpos=binarysearch(num,0,len(num)-1,target)
if firstpos==-1:
return -1
else:
startpos=firstpos
endpos=firstpos
temp1=0
temp2=0
while startpos!=-1:
temp1=startpos
startpos=binarysearch(num,0,startpos-1,target)
startpos=temp1
while endpos!=-1:
temp2=endpos
endpos=binarysearch(num,endpos+1,len(num)-1,target)
endpos=temp2
return [startpos,endpos]
if __name__==”__main__”:
x=occurance(num)
print(x[1]-x[0]+1)
package PrepInsta;
import java.util.Scanner;
public class Frequency2 {
public static int countfreq(int[] arr,int n,int num)
{
int count=0;
for(int i=0;i<n;i++)
{
if(arr[i]==num)
count++;
}
return count;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int[] arr= {2,5,6,2,4,5,6,1,3,6};
System.out.println("Enter number:");
int num=sc.nextInt();
int count=countfreq(arr,arr.length,num);
System.out.println(count);
}
}
#include
int main()
{
int n, a[100], count=0,i, num;
printf(“Enter a number\n”);
scanf(“%d”,&num);
printf(“enter the size of the array\n”);
scanf(“%d”,&n);
printf(“enter the array elements\n”);
for(i=0; i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n;i++)
{
if (num==a[i])
count++;
}
printf("%d ocuured %d times",num, count);
return 0;
}
#include
int main()
{
int ch[]={1, 1, 2, 2, 2, 2, 3,};
int input,size,count=0;
scanf(“%d”,&input);
size=sizeof(ch)/sizeof(int);
for(int i=0;i<size;i++)
{
if(input==ch[i])
{
count++;
}
}
if(count)
printf("%d",count);
else
printf("%d does not occure in given array",input);
return 0;
}
#include
int count_occurence(int arr[],int n,int m){
int count = 0;
for(int i=0;i<n;i++){
if(arr[i] == m){
count++;
}
}
printf("%d",count);
}
int main()
{
int n,m;
scanf("%d %d",&n,&m);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
count_occurence(arr,n,m);
return 0;
}
k=[1,2,2,3,4]
n=5
count=0
if n in k:
for i in range(len(k)):
if k[i]==n:
count+=1
print(count)
else:
print(“doesn’t occur in arr”)
def countocurrences(arr,n,x):
count = 0
for i in range(len(A)):
if A[i] ==x:
count+=1
return count
arr = [1,2,2,2,2,3,3,2,2,2,2]
x = 2
n = len(arr)
print(countocurrences(arr,n,x))