{{#items}}

{{text}}

{{/items}} {{ctaText}}

{{#items}} {{/items}}

showing results for - "count occurrences in seven integers using java single dimension arrays"

Camilo

29 Apr 2016

```
1import java.util.Scanner;
2
3public class SevenNumbersCount {
4
5 public static int count(int[] arr, int num) {
6 int count = 0;
7 for(int i = 0; i < arr.length; ++i) { // go through all elements in array
8 if(arr[i] == num) { // if num is found, increase count
9 count++; // increment count
10 }
11 }
12 return count; // return the count of number of occurrences of num in array
13 }
14
15 public static boolean checkIfElementAlreadyExists(int[] arr, int index) {
16 for(int i = 0; i < index; ++i) { // go through all elements in array
17 if(arr[i] == arr[index]) { // if item in index already exists in the array
18 return true; // then return true
19 }
20 }
21 return false; // if element at index does not previously occur, then return false
22 }
23
24 public static void main(String[] args) {
25 Scanner in = new Scanner(System.in);
26 int[] arr = new int[7];
27 System.out.print("Enter seven numbers: ");
28 for(int i = 0; i < arr.length; ++i) {
29 arr[i] = in.nextInt(); // read 7 numbers into array
30 }
31 for(int i = 0; i < arr.length; ++i) { // go through all elements
32 if(!checkIfElementAlreadyExists(arr, i)) { // if this element did not already appear before in the array
33 System.out.printf("Number %d occurs %d times.\n", arr[i], count(arr, arr[i])); // then print the number and the number of time it occurs in the array
34 }
35 }
36 }
37}
38
39
```

Paolo

12 May 2019

```
1public static int count(int[] arr, int num) {
2 int count = 0;
3 for(int i = 0; i < arr.length; ++i) { // go through all elements in array
4 if(arr[i] == num) { // if num is found, increase count
5 count++; // increment count
6 }
7 }
8 return count; // return the count of number of occurrences of num in array
9 }
10
11 public static boolean checkIfElementAlreadyExists(int[] arr, int index) {
12 for(int i = 0; i < index; ++i) { // go through all elements in array
13 if(arr[i] == arr[index]) { // if item in index already exists in the array
14 return true; // then return true
15 }
16 }
17 return false; // if element at index does not previously occur, then return false
18 }
19
20 public static void main(String[] args) {
21 Scanner in = new Scanner(System.in);
22 int[] arr = new int[7];
23 System.out.print("Enter seven numbers: ");
24 for(int i = 0; i < arr.length; ++i) {
25 arr[i] = in.nextInt(); // read 7 numbers into array
26 }
27 for(int i = 0; i < arr.length; ++i) { // go through all elements
28 if(!checkIfElementAlreadyExists(arr, i)) { // if this element did not already appear before in the array
29 System.out.printf("Number %d occurs %d times.\n", arr[i], count(arr, arr[i])); // then print the number and the number of time it occurs in the array
30 }
31 }
32 }
33}
34
```

similar questions

find out duplicate number between 1 to n numbers in javahow to count the number of occurrences of a character in an arraylist recursivelyfind the smallest length of a contiguous subarray of which the sum is greater than or equal to specified value in javacount number of matches in two strings javacount number of even and odd elements in an array in javajava 2c how to find the most repeated characterfind number of occurrences of a substring in a string javacounting repeated characters in a string in javafind sum of first and last digits in javacount the number of occurrences of a character in a string javajava program to find largest of three numbers using nested ifnumber of occurence string in javahow to count the number of occurrences of an element in a arraylist in javajava count elements in arraysmallest positive integer not in array javajava program to find the largest in three numbers using nested ifcompare index of two arrays return matches javajava find all instances of int in arryexample to check two integer array are equal in java 3fqueries leading to this page

count occurrences in seven integers using java single dimension arrayscount int occurrence in array javacount occurrences in seven integers using java two dimensional arrayscount occurrences in seven integers using java 2f two dimensional arrayscount occurrences in seven integers using java single dimension arrays your answer for

you will get a confirmation link on this - you will have to click that for successful submission of your answer. we require this to keep the website free of spam, bots and unhelpful content

please ensure to add code which is syntactically corrent and executes properly

ask question on maxinterview

you will get a confirmation link on this - you will have to click that for successful submission of your question. we require this to keep the website free of spam, bots and unhelpful content

please be clear, to the point and respectful