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 < 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
}
}
}
}
Thank you!
1
0
Are there any code examples left?
New code examples in category Java
-
Java 2022-03-27 21:35:04 Sort string array in case insensitive order and case sensitive order java
-
Java 2022-03-27 21:25:10 java -jar -l resources\es.porperties -i ejemplo.txt -o inject.bin
-
Java 2022-03-27 21:20:21 Debug & Fix a 2-Dimensional Array Java Console Application
-
Java 2022-03-27 20:40:19 TreeSet headSet(E toElement) method in java
-
Java 2022-03-27 19:30:06 close keyboard android
-
Java 2022-03-27 19:20:40 java measure execution time
-
Java 2022-03-27 19:10:06 how to add cardview support in android studio
-
Java 2022-03-27 18:55:08 android studio lower case letters on a button