count occurrences in seven integers using java single dimension arrays

Solutions on MaxInterview for count occurrences in seven integers using java single dimension arrays by the best coders in the world

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