count occurrences in seven integers using java single dimension arrays

import java.util.Scanner;

public class SevenNumbersCount {

    public static int count(int[] arr, int num) {
        int count = 0;
        for(int i = 0; i < arr.length; ++i) {   // go through all elements in array
            if(arr[i] == num) { // if num is found, increase count
                count++;    // increment count
            }
        }
        return count;   // return the count of number of occurrences of num in array
    }

    public static boolean checkIfElementAlreadyExists(int[] arr, int index) {
        for(int i = 0; i < index; ++i) {    // go through all elements in array
            if(arr[i] == arr[index]) {  // if item in index already exists in the array
                return true;    // then return true
            }
        }
        return false;   // if element at index does not previously occur, then return false
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] arr = new int[7];
        System.out.print("Enter seven numbers: ");
        for(int i = 0; i < arr.length; ++i) {
            arr[i] = in.nextInt();  // read 7 numbers into array
        }
        for(int i = 0; i < arr.length; ++i) {   // go through all elements
            if(!checkIfElementAlreadyExists(arr, i)) {  // if this element did not already appear before in the array
                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
            }
        }
    }
}


0
1

                                    public static int count(int[] arr, int num) {
        int count = 0;
        for(int i = 0; i &lt; arr.length; ++i) {   // go through all elements in array
            if(arr[i] == num) { // if num is found, increase count
                count++;    // increment count
            }
        }
        return count;   // return the count of number of occurrences of num in array
    }

    public static boolean checkIfElementAlreadyExists(int[] arr, int index) {
        for(int i = 0; i &lt; index; ++i) {    // go through all elements in array
            if(arr[i] == arr[index]) {  // if item in index already exists in the array
                return true;    // then return true
            }
        }
        return false;   // if element at index does not previously occur, then return false
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] arr = new int[7];
        System.out.print(&quot;Enter seven numbers: &quot;);
        for(int i = 0; i &lt; arr.length; ++i) {
            arr[i] = in.nextInt();  // read 7 numbers into array
        }
        for(int i = 0; i &lt; arr.length; ++i) {   // go through all elements
            if(!checkIfElementAlreadyExists(arr, i)) {  // if this element did not already appear before in the array
                System.out.printf(&quot;Number %d occurs %d times.\n&quot;, arr[i], count(arr, arr[i]));  // then print the number and the number of time it occurs in the array
            }
        }
    }
}

0
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source