How can i generate all subsets of a variable length set? - java

I am trying to write a program that generates all the subsets of an entered set in java. I think i nearly have it working.
I have to use arrays (not data structures)
The entered array will never be greater than 20
Right now when i run my code this is what i get:
Please enter the size of A: 3
Please enter A: 1 2 3
Please enter the number N: 3
Subsets:
{ }
{ 1 }
{ 1 2 }
{ 1 2 3 }
{ 2 3 }
{ 2 3 }
{ 2 }
{ 1 2 }
this is the correct number of subsets (2^size) but as you can see it prints a few duplicates and not some of the subsets.
Any ideas where I am going wrong in my code?
import java.util.Scanner;
public class subSetGenerator
{
// Fill an array with 0's and 1's
public static int [] fillArray(int [] set, int size)
{
int[] answer;
answer = new int[20];
// Initialize all elements to 1
for (int i = 0; i < answer.length; i++)
answer[i] = 1;
for (int a = 0; a < set.length; a++)
if (set[a] > 0)
answer[a] = 0;
return answer;
} // end fill array
// Generate a mask
public static void maskMaker(int [] binarySet, int [] set, int n, int size)
{
int carry;
int count = 0;
boolean done = false;
if (binarySet[0] == 0)
carry = 0;
else
carry = 1;
int answer = (int) Math.pow(2, size);
for (int i = 0; i < answer - 1; i++)
{
if (count == answer - 1)
{
done = true;
break;
}
if (i == size)
i = 0;
if (binarySet[i] == 1 && carry == 1)
{
binarySet[i] = 0;
carry = 0;
count++;
} // end if
else
{
binarySet[i] = 1;
carry = 1;
count++;
//break;
} // end else
//print the set
System.out.print("{ ");
for (int k = 0; k < size; k++)
if (binarySet[k] == 1)
System.out.print(set[k] + " ");
System.out.println("}");
} // end for
} // maskMaker
public static void main (String args [])
{
Scanner scan = new Scanner(System.in);
int[] set;
set = new int[20];
int size = 0;
int n = 0;
// take input for A and B set
System.out.print("Please enter the size of A: ");
size = scan.nextInt();
if (size > 0)
{
System.out.print("Please enter A: ");
for (int i = 0; i < size; i++)
set[i] = scan.nextInt();
} // end if
System.out.print("Please enter the number N: ");
n = scan.nextInt();
//System.out.println("Subsets with sum " + n + ": ");
System.out.println("Subsets: ");
System.out.println("{ }");
maskMaker(fillArray(set, size), set, n, size);
} // end main
} // end class

The value of i always goes from 0 to N-1 and then back to 0. This is not useful to generate every binary mask you need only one time. If you think about it, you need to move i only when you have generate all possible masks up to i-1.
There is a much easier way to do this if you remember every number is already internally represented in binary in the computer and everytime you increment it Java is doing the adding and carrying by itself. Look for bitwise operators.

Related

Printing spaces to align numbers according to my pyramid pattern

It sounds a lot easier than it looks. Basically I have my code finished this is my output where the leading number is whatever integer the program receives as input. In this case n = 5:
1
21
321
4321
54321
but this is what it is suppose to look like:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
How should I go about adding spaces in between my numbers while maintaining this pattern? I've tried editing here and there but it keeps coming out like this:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
My code:
import java.util.Scanner;
public class DisplayPattern {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer and I will display a pattern for you: ");
int n = input.nextInt();
displayPattern(n);
}
public static void displayPattern(int n) {
final int MAX_ROWS = n;
for (int row = 1; row <= MAX_ROWS; row++) {
for (int space = (n - 1); space >= row; space--) {
System.out.print(" ");
}
for (int number = row; number >= 1; number--) {
System.out.print(number + " "); /*<--- Here is the edit.*/
}
System.out.println();
}
}
Edit:
#weston asked me to display what my code looks like with the second attempt. It wasn't a large change really. All i did was add a space after the print statement of the number. I'll edit the code above to reflect this. Since it seems that might be closer to my result I'll start from there and continue racking my brain about it.
I managed to get the program working, however this only caters to single digit number (i.e. up to 9).
import java.util.Scanner;
public class Play
{
public static class DisplayPattern
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer and I will display a pattern for you: ");
int n = input.nextInt();
displayPattern(n);
}
public static void displayPattern(int n)
{
final int MAX_ROWS = n;
final int MAX_COLUMNS = n + (n-1);
String output = "";
for (int row = 1; row <= MAX_ROWS; row++)
{
// Reset string for next row printing
output = "";
for (int space = MAX_COLUMNS; space > (row+1); space--) {
output = output + " ";
}
for (int number = row; number >= 1; number--) {
output = output + " " + number;
}
// Prints up to n (ignore trailing spaces)
output = output.substring(output.length() - MAX_COLUMNS);
System.out.println(output);
}
}
}
}
Works for all n.
In ith row print (n-1 - i) * length(n) spaces, then print i+1 numbers, so it ends with 1 separated with length(n) spaces.
public static void printPiramide(int n) {
int N = String.valueOf(n).length();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1 - i; j++) {
for (int k = 0; k < N; k++)
System.out.print(" ");
}
for (int j = i+1; j > 0; j--) {
int M = String.valueOf(j).length();
for (int k = 0; k < (N - M)/2; k++) {
System.out.print(" ");
}
System.out.print(j);
for (int k = (N - M)/2; k < N +1; k++) {
System.out.print(" ");
}
}
System.out.println();
}
}
public class DisplayPattern{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer and I will display a pattern for you: ");
int n = input.nextInt();
List<Integer> indentList = new ArrayList<Integer>();
int maxLength= totalSpace(n) + (n-1);
for(int i = 1; i <= n; i++ ){
int eachDigitSize = totalSpace(i);
int indent = maxLength - (eachDigitSize+i-1);
indentList.add(indent);
}
for(int row = 1; row<=n; row++){
int indentation = indentList.get(row-1);
for(int space=indentation; space>=0; space--){
System.out.print(" ");
}
for(int number = row; number > 0; number--){
System.out.print(number + " ");
}
System.out.println();
}
}
private static int totalSpace(int n) {
int MAX_ROWS = n;
int count = 0;
for(int i = MAX_ROWS; i >= 1; i--){
int currNum = i;
int digit;
while(currNum > 0){
digit=currNum % 10;
if(digit>=0){
count++;
currNum = currNum/10;
}
}
}
return count;
}
}
It works properly for any number of rows(n).
java-8 solution to the problem:
IntStream.rangeClosed(1, MAX)
.forEach(i -> IntStream.range(0, MAX)
.map(j -> MAX - j)
.mapToObj(k -> k == 1 ? k + "\n" : k <= i ? k + " " : " ")
.forEach(System.out::print)
);
Output for MAX = 5:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
For the bottom row, you have the right number of spaces. But for the next row from the bottom, you're missing one space on the left (the 4 is out of line by 1 space). In the next row up, you're missing two spaces on the left (the 3 is out of line by 2 spaces)... and so on.
You're adding a number of spaces to the beginning of each line, but you're only taking into account the number of digits you're printing. However, you also need to take into account the number of spaces you're printing in the previous lines.
Once you get that part working, you might also consider what happens when you start to reach double-digit numbers and how that impacts the number of spaces. What you really want to do is pad the strings on the left so that they are all the same length as the longest line. You might check out the String.format() method to do this.

How to find the mode of an array using Java

I'm trying to find the mode for my program, a user inputs an amount of numbers from 0-100 as many as they want, I'm attempting to find the mode of these numbers but each time I attempt to find the mode it gives me back a 3, I've found everything else, I just need the help with the mode.
import java.util.Scanner;
public class deveation {
public static void main(String Args[]) {
Scanner kbReader = new Scanner(System.in);
int sum = 0;
int bob[] = new int[101];
int total = 0;
int a = 0;
int min = 0;
int max = 100;
int mode = 0;
boolean stay_in_loop = true;
while (stay_in_loop) {
System.out.println("Please enter interger(s) from 0-100: ");
int number = kbReader.nextInt();
if (number < 0) {
stay_in_loop = false;
}
else {
total++;
bob[number]++;
}
}
int median = total / 2 + 1;
while (median > 0) {
median -= bob[a];
a++;
}
a--;
boolean findit = true;
while (findit) {
if (bob[min] != 0)
findit = false;
else
min++;
}
boolean findme = true;
while (findme) {
if (bob[max] != 0)
findme = false;
else
max--;
}
for (int p = 0; p < 101; p++) {
if (bob[p] > mode) {
mode = bob[p];
}
for (int j = 0; j < 101; j++)
if (bob[j] <= mode)
//I don't know why I'm getting three for this
{
}
}
for (int i = 0; i < 101; i++) {
sum += bob[i] * i;
}
System.out.println(sum);
System.out.println(sum /= total);
System.out.println(a);
System.out.println(min);
System.out.println(max);
System.out.println(mode);
//You should start putting down these comments
}
}
Mode is the number(s) that is repeated most often. I would get rid of the inner for loop that you have.
for (int p = 0; p<101; p++) {
if (bob[p]>mode) {
mode=bob[p];
}
}
I am not sure why you say you are always getting three. At the end of the above loop, the mode variable will contain the largest count for a number in your bob array.
You can then loop back through the list (or store the values while looping through it) and print out the numbers that have a count that match your the value of your mode.
for (int p = 0; p < 101; p++) {
if (bob[p] == mode) {
System.out.println("Mode Number: " + p);
}
}
Remember that mode can be more than one number.
You make mode = bob[p], but bob[p] is just how many times the number appeared in your array. The mode should really be p.
For example, suppose that the bob array is:
[2, 1, 3, 1, 1, 2]
This means that 0 appears twice, 1 appears once, 2 appears three times and so on. The mode in this case is 2, which is given by the array index, and not by the value stored in the array.
To find the mode, then, we need to loop through the count array (bob), and keep two variables, the mode and the highest count until now. There is no need to loop twice or use nested loops.
int count = 0;
int mode = 0;
for (int p = 0; p < bob.length; p++) {
// If the count of the p element is greater than the greatest count until now
if (bob[p] > count) {
// Update the greatest count
count = bob[p];
// p is the new mode
mode = p;
}
}
Try using something like a hashmap where the key would the number and the value would be the number of occurences. And you can just keep track of the highest value. Also you should be able to do this in O(n) time meaning in one loop through the array. There are plenty of examples online of finding the mode.

Random Dice Generator

A run is a sequence of adjacent repeated values . Write a program that generates a sequence of random die tosses and that prints the die values, marking only the longest run. The program should take as input the total number of die tosses (ex 10), then print:
1 6 6 3 (2 2 2 2 2) 5 2
Im quite confused on how to compare each number in order to get the correct output. Maybe using an array to store the values. Any answers or input will be of help thank you!
import java.util.Random;
import java.util.Scanner;
public class Dice
{
Random generator = new Random();
Scanner keyboard = new Scanner(System.in);
public void DiceCount()
{
int count;
int sides = 6;
int number;
System.out.println("How many die? ");
count = keyboard.nextInt();
for(int i=0; i < count; i++)
{
number = generator.nextInt(sides);
System.out.print(number);
}
}
}
First, replace int number; with int[] numbers = new int[count];. Next, replace number = ... with numbers[i] = ....
This will give you an array of random numbers (don't print them yet!). As you generate your numbers, note how many equal numbers you get in a row (add a special counter for that). Also add variable that stores the length of the longest run so far. Every time you get a number that's equal to the prior number, increment the counter; otherwise, compare the counter to the max, change the max if necessary, and set the counter to 1. When you update the max, mark the position where the run starts (you can tell from the current position and the length of the run).
Now it's time to detect the longest run: go through the numbers array, and put an opening parenthesis where the run starts. Put a closing parenthesis when you reach the end of the run, and finish the printing to complete the output for the assignment.
import java.util.Random;
import java.util.Scanner;
public class Dice {
Random generator = new Random();
Scanner keyboard = new Scanner(System.in);
public void DiceCount() {
int sides = 6;
System.out.println("How many die? ");
int count = keyboard.nextInt();
int[] array = new int[count];
int longestLength = 1, currentLength = 1, longestLengthIndex = 0, currentLengthIndex = 1;
int currentNum = -1;
for (int i = 0; i < count; i++) {
array[i] = generator.nextInt(sides);
System.out.print(array[i] + " ");
if (currentNum == array[i]) {
currentLength++;
if (currentLength > longestLength) {
longestLengthIndex = currentLengthIndex;
longestLength = currentLength;
}
} else {
currentLength = 1;
currentLengthIndex = i;
}
currentNum = array[i];
}
System.out.println();
for (int i = 0; i < count; i++)
System.out.print((i == longestLengthIndex ? "(" : "") + array[i] + (i == (longestLengthIndex + longestLength - 1) ? ") " : " "));
}
}
Note: this will only take the first longest range. So if you have 1123335666 it will do 112(333)5666.
If you need 112(333)5(666) or 1123335(666) then I leave that to you. It's very trivial.
import java.util.Random;
public class Dice {
public static void main(String[] args) {
//make rolls
Random rand = new Random();
int[] array = new int[20];
int longestRun = 1;
int currentRun = 1;
int longestRunStart = 0;
int currentRunStart = 1;
System.out.print("Generated array: \n");
for (int i = 0; i < array.length; i++) {
array[i] = rand.nextInt(6); //add random number
System.out.print(array[i] + " "); //print array
if (i != 0 && array[i - 1] == array[i]) {
//if new number equals last number...
currentRun++; //record current run
if (currentRun > longestRun) {
longestRunStart = currentRunStart; //set index to newest run
longestRun = currentRun; //set above record to current run
}
} else {
//if new number is different from the last number...
currentRun = 1; //reset the current run length
currentRunStart = i; //reset the current run start index
}
}
//record results
System.out.print("\nIdentifying longest run: \n");
for (int i = 0; i < longestRunStart; i++) { System.out.print(array[i] + " "); } //prints all numbers leading up to the run
System.out.print("( "); //start parentheses
for (int i = longestRunStart; i < (longestRunStart + longestRun); i++) { System.out.print(array[i] + " "); } //prints the run itself
System.out.print(") "); //end parentheses
for (int i = (longestRunStart + longestRun); i < 20; i++) { System.out.print(array[i] + " "); } //all remaining numbers
}
}```

stop a loop from asking user input

problem here is that we want the program to stop the loop/stop asking integers when '-1' is inputted by the user that it wont have to get the maximum length of our array
import java.util.Scanner;
public class DELETE DUPLICATES {
public static void main(String[] args) {
UserInput();
getCopies(maxInput);
removeDuplicates(maxInput);
}
static int[] maxInput= new int[20];
static int[] copies = new int[20];
static int[] duplicate = new int[20];
//get user's input/s
public static void UserInput() {
Scanner scan = new Scanner(System.in);
int integer = 0;
int i = 0;
System.out.println("Enter Numbers: ");
while(i < maxInput.length)
{
integer = scan.nextInt();
maxInput[i] = integer;
i++;
if (integer == -1)
break;
}
int j = 0;
for(int allInteger : maxInput) {
System.out.print(allInteger+ " ");
j++;
}
}
//to get value/number of copies of the duplicate number/s
public static void getCopies(int[] Array) {
for(int allCopies : Array) {
copies[allCopies]++;
}
for(int k = 0; k < copies.length; k++) {
if(copies[k] > 1) {
System.out.print("\nValue " + k + " : " + copies[k] + " copies are detected");
}
}
}
//remove duplicates
public static void removeDuplicates(int[] Array) {
for(int removeCopies : Array) {
duplicate[removeCopies]++;
}
for(int a = 0; a < duplicate.length; a++) {
if(duplicate[a] >= 1) {
System.out.print("\n"+a);
}
}
}
}
Example:
If we input :
1
2
3
3
4
5
-1
The result of our program is : 1 2 3 3 4 5 -1 0 0 0 0 0 0 0 0 0 0 0 0 0
We want the result to be like: 1 2 3 3 4 5
We need your help guyz . practicing our programming 1 subject hope we could get some help here
You can do a following change just to print the required values:
for(int allInteger : maxInput)
{
if(allInteger == -1)
break;
System.out.print(allInteger+ " ");
j++;
}
but, better change would be to rethink your design and use of data structures.
The maxInput array is of specified size that is 20, so it will have that no. of elements and prints the default value of int.
You can use List instead and check the size for max input and exit loop
If you don't need to use an array, then a Collection has lots of advantages. Let's use a List:
static int maxInputCount = 20;
static ArrayList<Integer> input= new ArrayList<>();
...
for (int i = 0; i < maxInputCount; i++)
{
integer = scan.nextInt();
if (integer == -1)
break;
input.add(integer);
}
for(int integer : input) {
System.out.print(integer+ " ");
}

Finding Multiple Modes In An Array

I'm trying to write a java method which finds all the modes in an array. I know there is a simple method to find the mode in an array but when there are more than one single mode my method outputs only one of them. I've tried to find a way but am nit sure how to approach this problem. Can anyone help me out to find all the modes in the array? Thanks.
Yes here is my code which outputs only one mode even if multiple modes exist.
public static int mode(int a[]){
int maxValue=0, maxCount=0;
for (int i = 0; i < a.length; ++i){
int count = 0;
for (int j = 0; j < a.length; ++j){
if (a[j] == a[i]) ++count;
}
if (count > maxCount){
maxCount = count;
maxValue = a[i];
}
}
return maxValue;
}
okay here's an example:
30
30
30
34
34
23
In this set of numbers there is only one mode, which is 30.
30
30
30
34
34
34
23
But in this set there are two modes, 30 and 34. I want my code to be able to output both of them, whereas it only prints one. It prints only 30.
The following code will return you an Integer[] containing the modes. If you need an int[] instead, you still need to convert the Integer instances to ints manually. Probably not the most efficient version, but its matches closely to your code
public static Integer[] mode(int a[]){
List<Integer> modes = new ArrayList<Integer>( );
int maxCount=0;
for (int i = 0; i < a.length; ++i){
int count = 0;
for (int j = 0; j < a.length; ++j){
if (a[j] == a[i]) ++count;
}
if (count > maxCount){
maxCount = count;
modes.clear();
modes.add( a[i] );
} else if ( count == maxCount ){
modes.add( a[i] );
}
}
return modes.toArray( new Integer[modes.size()] );
}
After a long night of programming, I finality got a program that will print out the mode/modes of an array. Or will even tell you if there isn't a mode (say, if no input occurred more than once or all the inputs occurred the same amount of times: ex. 1, 1, 2, 2, 3, 3, 4, 4). Some limitations of the program are that you must enter more than one number, and you cannot enter more than 10000 numbers or a negative number (if you wanted to enter a negative number, you would just have to tweak all the for loops involving the values[][] array. Some cool things about my program are that it prints out how many times each of you inputs occurred along with the mode of your array. And all of the print outs grammar change according to the amount of info (Ex. The mode of your array is 2; The modes of your array are 1 & 2; The modes of your array are 0, 2, 5, & 8). There is also a bubble sort function example in the program for anyone who thought that they needed a sorter function in their mode program. Hope this helps, I included a lot of pseudo code to help anyone who doesn't see how my logic progress throughout the program. (FYI: It is java, and was compiled in BlueJ)
import java.util.Scanner;
public class Mode
{
public static void main (String args [])
{
Scanner scan = new Scanner(System.in);
int MAX_INPUTS = 10000; boolean flag = false;
System.out.print ("Input the size of your array: ");
int size; // How many nubers will be in the user array
do
{
size = scan.nextInt();
if (size == 1)
{
System.out.print ("\nError. You must enter a number more than 1.\n\n");
continue;
}
else if (size > MAX_INPUTS || size < 0)
{
System.out.print ("\nError. You muste enter a number less than " + MAX_INPUTS + " or greater than 0\n\n");
continue;
}
else
flag = true; // a ligit answer has been entered.
}
while (flag != true);
int array[] = new int[size], values[][] = new int[2][MAX_INPUTS + 1], ticks = 0;
System.out.print ("\nNow input the numbers for your array.\n\n");
/* Taking inputs from the user */
while (ticks < size)
{
System.out.print ("Number " + (ticks + 1) + ": ");
array[ticks] = scan.nextInt();
if (array[ticks] > MAX_INPUTS || array[ticks] < 0)
{
System.out.print ("\nError. Number cannot be greater than " + MAX_INPUTS + " or less than 0\n\n");
continue;
}
++ticks;
}
/*
* values[][] array will hold the info for how many times numbers 0 - 10000 appear in array[]. Column 0 will hold numbers from 0 -1000, and column 1 will hold the number of
* of repititions the number in column 0 occured in the users inputed array.
*/
for (int i = 0; i < MAX_INPUTS; ++i) // Initalize Column zero with numbers starting at zeor, and ending and MAX_INPUTS.
values[0][i] = i;
for (int i = 0; i < size; ++i) // Find the repatitions of the numbers in array[] that correspond to the number in column zere of values[][].
for (int j = 0; j < MAX_INPUTS; ++j)
if (array[i] == j)
++values[1][j];
sort (array, size);
System.out.print ("\n\nHere are the numbers you entered.\n\n"); // show the values the user entered in ascending order.
for (int i = 0; i < size; ++i)
{
if (i == size - 1) // the last inputed number
System.out.print (array[i]); // don't allow an extra comma.
else
System.out.print (array[i] + ", ");
}
// Show the user how many times each of the values he/she entered occured.
System.out.print ("\n\nThis is the amount of times each of the values you entered occured:\n");
for (int i = 0; i < MAX_INPUTS; ++i)
{
if (values[1][i] == 1)
System.out.print (i + " was entered " + values[1][i] + " time\n"); // avoid: 2 was entered 1 times
else if (values[1][i] != 0)
System.out.print (i + " was entered " + values[1][i] + " times\n"); // avoid: 2 was entered 2 time
}
/* -------------------------------------------------------------------- | Finding the Mode/Modes | -------------------------------------------------------------------- */
/* The process begins with creating a second array that is the exactly the same as the values[][] (First for loop). Then I sort the duplicate[] array to find the mode
* (highest number in the duplicate[]/values[][] arrays. Int max is then assigned the highest number. Remembering that the values[][] array: column 0 contains numbers ranging
* from 1 to 10000, it keeps track of where the numbers in column were originally located, in which you can compare to the duplicate array which is sorted. Then I can set
* up a flag that tells you whether there is more than one mode. If so, the printing of these modes will look neater and the grammar can be changed accordingly.
*/
int duplicate[] = new int [10001], mode[] = new int [size], max, mode_counter = 0;
boolean multi_mode = false, all_same;
for (int i = 0; i < MAX_INPUTS; ++i)
duplicate[i] = values[1][i]; // copy values array.
sort (duplicate, MAX_INPUTS);
max = duplicate[MAX_INPUTS - 1]; // the last number in the sorted array is the greatest.
all_same = test (duplicate, MAX_INPUTS, size, max); // this is the test to see if all the numbers in the user array occured the same amount of times.
int c = 0; // a counter
/* The mode of the user inputed array will be recorded in the values array. The sort of the duplicate array told me what was the higest number in that array. Now I can
* see where that highest number used to be in the original values array and recored the corresponding number in the column zero, which was only filled with numbers 0 -
* 10000. Thus telling me the mode/modes.
*/
for (int i = 0; i < MAX_INPUTS; ++i)
{
if (values[1][i] == max)
{
mode[c++] = values[0][i];
++mode_counter;
}
}
if (mode[1] != 0) //mode[0] (the first cell, has a number stored from the last for loop. If the second cell has a number other than zero, that tells me there is more than 1 mode.
multi_mode = true;
if (multi_mode == false)
System.out.print ("\nThe mode of your array is " + mode[0]); // For correct grammer.
else if (all_same == true)
System.out.print ("\nAll of the numbers entered appeared the same amount of times. "); // See the boolean function for more details
else // If here there is more than one mode.
{
System.out.print ("\nThe modes of yoru array are ");
for (int i = 0; i < mode_counter; ++i)
{
if (mode_counter > 2 && i == (mode_counter - 1)) // If there is more than two modes and the final mode is to be printed.
System.out.print ("& " + mode[i]);
else if (mode_counter == 2)
{ // This is true if there is two modes. The else clause will print the first number, and this will print the amper sign and the second mode.
System.out.print (mode[0] + " & " + mode[1]);
break;
}
else
System.out.print (mode[i] + ", ");
}
}
}
public static void sort (int list[], int max) // Its the bubble sort if you're wondering.
{
int place, count, temp;
for (place = 0; place < max; ++place)
for (count = max - 1; count > place; --count)
if (list[count - 1] > list[count])
{
temp = list[count-1];
list[count - 1] = list[count];
list[count] = temp;
}
}
/* The test to see if there isn't a mode. If the amount of the mode number is the same as the amount of numbers there are in the array is true, or if the size entered by the
* user (input) modulo the mode value is equal to zero (say, all the numbers in an array of ten were entered twice: 1, 1, 2, 2, 3, 3, 4, 4, 5, 5). */
public static boolean test (int list[], int limit, int input, int max)
{
int counter = 0, anti_counter = 0;
for (int i = 0; i < limit; ++i)
if (list[i] == max)
++counter; // count the potential modes
else if (list[i] !=0 && list[i] != max)
++anti_counter; // count every thing else except zeros.
if (counter == input || (input % max == 0 && anti_counter == 0) )
return true;
else
return false;
}
}
Even though this probably isn't the most efficient solution, it will print all the highest modes:
public static int mode(int a[]) {
int maxValue=-1, maxCount=0;
for (int i = 0; i < a.length; i++) {
int count = 0;
for (int j = 0; j < a.length; j++) {
if (a[j] == a[i])
count++;
}
}
if (count > maxCount) {
maxCount = count;
maxValue = a[i];
}
}
//loop again and print only the highest modes
for (int i = 0; i < a.length; i++) {
int count = 0;
for (int j = 0; j < a.length; j++) {
if (a[j] == a[i])
count++;
}
}
if (count == maxCount) {
System.out.println(a[i]);
}
}
}
Hope this helps. This is my answer. Hope it helps
public List mode(double[] m) {
HashMap<Double, Double> freqs = new HashMap<Double, Double>();
for (double d : m) {
Double freq = freqs.get(d);
freqs.put(d, (freq == null ? 1 : freq + 1));
}
List<Double> mode = new ArrayList<Double>();
List<Double> frequency = new ArrayList<Double>();
List<Double> values = new ArrayList<Double>();
for (Map.Entry<Double, Double> entry : freqs.entrySet()) {
frequency.add(entry.getValue());
values.add(entry.getKey());
}
double max = Collections.max(frequency);
for(int i=0; i< frequency.size();i++)
{
double val =frequency.get(i);
if(max == val )
{
mode.add(values.get(i));
}
}
return mode;
}
Handling multiple mode values:
Here initially I am taking a map to get the frequencies of each element. After finding the max repeating value I am storing its map value.
While iterating to the map I am checking if this value is repeating for some key and is not 1, if it is then I am adding that key as well. This will get me the multiple modes
public static void mode(int a[]){
int count=0;
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<a.length;i++)
{
map.put(a[i],map.getOrDefault(a[i],0)+1);
}
Set<Integer> set=new HashSet<>();
int maxValue=0;
for(int i=0;i<a.length;i++)
{
int ct=0;
for(int j=0;j<a.length;j++)
{
if(a[i]==a[j])
ct++;
}
if(ct>count)
{
count=ct;
maxValue=a[i];
}
}
set.add(maxValue);
int k=map.get(maxValue);
if(k!=1)
{
for(Map.Entry m:map.entrySet())
{
if((int)m.getValue()==k)
set.add((int)m.getKey());
}
}
System.out.println("Mode: "+set);
}

Categories