brel-logo-white brel-logo-white
  • Travel
  • Case Study
  • How to
  • Technology
  • More
    • Sports
    • Health
    • Articles
    • Review
Notification
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
BreldigitalBreldigital
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
Search
  • Quick Access
    • Home
    • History
    • My Saves
    • My Interests
    • My Feed
  • Categories
    • Travel
    • Sports
    • Health

Top Stories

Explore the latest updated news!

What idea is emphasized through repetition

The a value of a function in the form f(x) = ax2 + bx + c is negative. which statement must be true?

What experimental evidence led to the development of this atomic model from the one before it?

Stay Connected

Find us on socials
248.1k Followers Like
61.1k Followers Follow
165k Subscribers Subscribe
Breldigital > Blog > ds > Count occurrence of numbers java
ds

Count occurrence of numbers java

By John Published November 9, 2022
Share

This is a Java Program to Count the Number of Occurrence of an Element in an Array.
Enter size of array and then enter all the elements of that array. Now enter the element of which you want to count occurrences. With the help of for loop now we can easily calculate number of occurrences of the given element.

Here is the source code of the Java Program to Count the Number of Occurrence of an Element in an Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

import java.util.Scanner;
public class Count_Occurrence
{
public static void main(String[] args)
{
int n, x, count = 0, i = 0;
Scanner s = new Scanner(System.in);
System.out.print(“Enter no. of elements you want in array:”);
n = s.nextInt();
int a[] = new int[n];
System.out.println(“Enter all the elements:”);
for(i = 0; i < n; i++) { a[i] = s.nextInt(); } System.out.print("Enter the element of which you want to count number of occurrences:"); x = s.nextInt(); for(i = 0; i < n; i++) { if(a[i] == x) { count++; } } System.out.println("Number of Occurrence of the Element:"+count); } } Output: $ javac Count_Occurrence.java $ java Count_Occurrence Enter no. of elements you want in array:5 Enter all the elements: 2 3 3 4 3 Enter the element of which you want to count number of occurrences:3 Number of Occurrence of the Element:3

TAGGED: Count occurrence of numbers java
John November 9, 2022 November 9, 2022

Search

brel-logo-white brel-logo-white
Explore a wide range of brands and categories with our comprehensive coverage, and stay up-to-date with the latest news and trends by subscribing to our updates.
Categories:
  • Entertainment
  • Travel
  • Sport
  • Contact Us

Quick Links

  • My Feed
  • My Interests
  • History
  • My Saves

About US

  • Adverts
  • Our Jobs
  • Term of Use

© 2023 Brel Digital All Rights Reserved. All logos and images used on this website are registered trademarks of their respective companies

Welcome Back!

Sign in to your account

Lost your password?