Ordering a vector of randomly generated numbers - java

I´m trying to solve a question but i can´t find why my code is not working for this problem. I have generated a random vector of 100 elements and im trying to order them into another. Somehow, my new generated vector is filled with the last index value of the random vector.
int[] vetorAleatory = new int[100];
for (int i = 0; i < vetorAleatory.length; i++) {
vetorAleatory[i] = new Random().nextInt(1000);
}
int[] vetorByOrder = new int[100];
int newVetorPosition = 0;
for (int i = 0; i < 100; i++) {
for (int x = 0; x < 100; x++) {
vetorByOrder[newVetorPosition] = 2000;
if (vetorAleatory[i] < vetorByOrder[newVetorPosition]) {
boolean newEntry = true;
for (int y = 0; y < newVetorPosition; y++) {
if (vetorByOrder[y] == vetorByOrder[newVetorPosition]) {
newEntry = false;
break;
}
}
if (newEntry == true) {
vetorByOrder[newVetorPosition] = vetorAleatory[x];
}
}
if (x == 99) {
newVetorPosition++;
}
}
}
for (int i = 0;i<100;i++) {
System.out.print(vetorAleatory[i] + ", " + vetorByOrder[i] + System.lineSeparator());
}

First you do not need 3 loops to sort an array. You need only 2 and in case of quick search, it is even less than that. You can check this example Array sort and search, or you can use built in Arrays.sort method in Java

Related

Array with bubble sort and odd/even list - Anyone knows how to remove duplicate element in my even list?

Regardless of the programming language you use. You will undoubtedly encounter data while dealing with arrays, particularly duplicates, that you will want to get rid of.
This is my Output
I already inputted the correct syntax of even list array is there something that I missed?
Anyone knows how to remove duplicate element in my even list?
Here's my code:
import java.util.*;
import java.util.Iterator;
public class ArrayBubbleSortwithOddandEven {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int size, temp;
System.out.print("Size of the list: ");
size = scanner.nextInt();
int number[] = new int[size];
int oddarray[] = new int[size];
int evenarray[] = new int[size];
int odd = 0;
int even = evenarray.length - 1;
for (int i = 0; i < number.length; i++) {
System.out.print("Input: ");
number[i] = scanner.nextInt();
}
for (int i = 0; i < size; i++) {
for (int swap = 1; swap < (size - i); swap++) {
if (number[swap - 1] > number[swap]) {
temp = number[swap - 1];
number[swap - 1] = number[swap];
number[swap] = temp;
}
}
}
for (int i = 0; i < number.length; i++) {
if (number[i] % 2 != 0) {
oddarray[odd++] = number[i];
} else {
evenarray[even--] = number[i];
}
}
for (int i = 0; i < number.length; i++) {
evenarray[even] = evenarray[i];
}
for (int i = 0; i < number.length; i++) {
oddarray[odd] = oddarray[i];
}
for (int i = 0; i < size; i++) { //Bubble sort
for (int swap = 1; swap < (size - i); swap++) {
if (evenarray[swap - 1] > evenarray[swap]) {
temp = evenarray[swap - 1];
evenarray[swap - 1] = evenarray[swap];
evenarray[swap] = temp;
}
}
}
System.out.println("Odd List:"); //Print Odd list
for (odd = 0; odd < oddarray.length; odd++) {
System.out.println("List " + odd + ": " + (oddarray[odd]));
}
System.out.println("Even List:"); //Print Even list
for (even = 0; even < evenarray.length; even++) {
System.out.println("List " + even + ": " + (evenarray[even]));
}
}
}
you can use a Set\HashMap to remember which number you already discover during your iteration. GOOD LUCK !
You can read about Data Structures - just google it :).
here a few questions that might help you understand.
Data Structures for hashMap, List and Set
Why key in HashMap can't be duplicated
how to find duplicate item position from set in java

JAVA - Compare two arrays and create a new array with only the unique values from the first

I have to solve an exercise with the following criteria:
Compare two arrays:
int[] a1 = {1, 3, 7, 8, 2, 7, 9, 11};
int[] a2 = {3, 8, 7, 5, 13, 5, 12};
Create a new array int[] with only unique values from the first array. Result should look like this: int[] result = {1,2,9,11};
NOTE: I am not allowed to use ArrayList or Arrays class to solve this task.
I'm working with the following code, but the logic for the population loop is incorrect because it throws an out of bounds exception.
public static int[] removeDups(int[] a1, int[] a2) {
//count the number of duplicate values found in the first array
int dups = 0;
for (int i = 0; i < a1.length; i++) {
for (int j = 0; j < a2.length; j++) {
if (a1[i] == a2[j]) {
dups++;
}
}
}
//to find the size of the new array subtract the counter from the length of the first array
int size = a1.length - dups;
//create the size of the new array
int[] result = new int[size];
//populate the new array with the unique values
for (int i = 0; i < a1.length; i++) {
int count = 0;
for (int j = 0; j < a2.length; j++) {
if (a1[i] != a2[j]) {
count++;
if (count < 2) {
result[i] = a1[i];
}
}
}
}
return result;
}
I would also love how to solve this with potentially one loop (learning purposes).
I offer following soulution.
Iterate over first array, and find out min and max it's value.
Create temporary array with length max-min+1 (you could use max + 1 as a length, but it could follow overhead when you have values e.g. starting from 100k).
Iterate over first array and mark existed values in temorary array.
Iterate over second array and unmark existed values in temporary array.
Place all marked values from temporary array into result array.
Code:
public static int[] getUnique(int[] one, int[] two) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < one.length; i++) {
min = one[i] < min ? one[i] : min;
max = one[i] > max ? one[i] : max;
}
int totalUnique = 0;
boolean[] tmp = new boolean[max - min + 1];
for (int i = 0; i < one.length; i++) {
int offs = one[i] - min;
totalUnique += tmp[offs] ? 0 : 1;
tmp[offs] = true;
}
for (int i = 0; i < two.length; i++) {
int offs = two[i] - min;
if (offs < 0 || offs >= tmp.length)
continue;
if (tmp[offs])
totalUnique--;
tmp[offs] = false;
}
int[] res = new int[totalUnique];
for (int i = 0, j = 0; i < tmp.length; i++)
if (tmp[i])
res[j++] = i + min;
return res;
}
For learning purposes, we won't be adding new tools.
Let's follow the same train of thought you had before and just correct the second part:
// populate the new array with the unique values
for (int i = 0; i < a1.length; i++) {
int count = 0;
for (int j = 0; j < a2.length; j++) {
if (a1[i] != a2[j]) {
count++;
if (count < 2) {
result[i] = a1[i];
}
}
}
}
To this:
//populate the new array with the unique values
int position = 0;
for (int i = 0; i < a1.length; i++) {
boolean unique = true;
for (int j = 0; j < a2.length; j++) {
if (a1[i] == a2[j]) {
unique = false;
break;
}
}
if (unique == true) {
result[position] = a1[i];
position++;
}
}
I am assuming the "count" that you implemented was in attempt to prevent false-positive added to your result array (which would go over). When a human determines whether or not an array contains dups, he doesn't do "count", he simply compares the first number with the second array by going down the list and then if he sees a dup (a1[i] == a2[j]), he would say "oh it's not unique" (unique = false) and then stop going through the loop (break). Then he will add the number to the second array (result[i] = a1[i]).
So to combine the two loops as much as possible:
// Create a temp Array to keep the data for the loop
int[] temp = new int[a1.length];
int position = 0;
for (int i = 0; i < a1.length; i++) {
boolean unique = true;
for (int j = 0; j < a2.length; j++) {
if (a1[i] == a2[j]) {
unique = false;
break;
}
}
if (unique == true) {
temp[position] = a1[i];
position++;
}
}
// This part merely copies the temp array of the previous size into the proper sized smaller array
int[] result = new int[position];
for (int k = 0; k < result.length; k++) {
result[k] = temp[k];
}
Making your code work
Your code works fine if you correct the second loop. Look at the modifications I did:
//populate the new array with the unique values
int counter = 0;
for (int i = 0; i < a1.length; i++) {
for (int j = 0; j < a2.length; j++) {
if (a1[i] == a2[j]) {
result[counter] = a1[i];
counter++;
}
}
}
The way I would do it
Now, here is how I would create a method like this without the need to check for the duplicates more than once. Look below:
public static int[] removeDups(int[] a1, int[] a2) {
int[] result = null;
int size = 0;
OUTERMOST: for(int e1: a1) {
for(int e2: a2) {
if(e1 == e2)
continue OUTERMOST;
}
int[] temp = new int[++size];
if(result != null) {
for(int i = 0; i < result.length; i++) {
temp[i] = result[i];
}
}
temp[temp.length - 1] = e1;
result = temp;
}
return result;
}
Instead of creating the result array with a fixed size, it creates a new array with the appropriate size everytime a new duplicate is found. Note that it returns null if a1 is equal a2.
You can make another method to see if an element is contained in a list :
public static boolean contains(int element, int array[]) {
for (int iterator : array) {
if (element == iterator) {
return true;
}
}
return false;
}
Your main method will iterate each element and check if it is contained in the second:
int[] uniqueElements = new int[a1.length];
int index = 0;
for (int it : a1) {
if (!contains(it, a2)) {
uniqueElements[index] = it;
index++;
}
}

Check duplicated value in array java, android studio

I would like to check duplication in the finalChallenges ArrayList. Actually,I'm using while cicle for fill up the finalChallenges Arraylist. If cycle the finalChallenges arrayList contains duplication, then remove it and go forward.
Random random = new Random();
while (finalChallenges.size()<21) {
int index = random.nextInt(listChallenges.size());
finalChallenges.add(listChallenges.get(index));
if(compare(finalChallenges)){
inalChallenges.remove(index);
}
}
compare method:
public boolean compare(ArrayList<Challenges> compArray) {
for (int j=0;j<compArray.size();j++) {
for (int k = j + 1; k < compArray.size(); k++) {
if (k != j && compArray.get(k) == compArray.get(j))
return true;
}
}
return false;
}
Random random = new Random();
final Set<Challenges> challenges = new HashSet<>();
while (challenges.size() < 21) {
challenges.add(listChallenges.get(random.nextInt(listChallenges.size()));
}
finalChallenges = new ArrayList<>(challenges);
Better solution:
List<Integer> indices = new ArrayList<>(listChallenges.size());
List<Challenges> finalChallenges = new ArrayList<>();
for (int i = 0; i < listChallenges.size(); indices.add(i++));
for (int i = 0; i < Math.min(21, listChallenges.size()); ++i) {
int index = indices.remove(random.nextInt(indices.size());
finalChallenges.add(listChallenges.get(index));
}
Random random = new Random();
ArrayList<Challenges> temp = new ArrayList<Challenges>(listChallenges.size());
for (Challenges item : listChallenges) temp.add(item);
while (finalChallenges.size()<21 && temp.size()>0) {
int index = random.nextInt(temp.size());
finalChallenges.add(temp.get(index));
temp.remove(index);
}

Bubblesort Java, new to Java [duplicate]

This question already has answers here:
Sort an array in Java
(19 answers)
Closed 7 years ago.
public class App {
public static void main( String[] args ) {
int[] set = {0,0,0,0,0,0,0};
for(int i = 0; i < 7; i++) {
boolean unique = false;
while (!unique) {
int j = 0;
try {
j = JRandom.randInt(1, 45);
}
catch (ParamterException e) {
e.printStackTrace();
}
unique = true;
for(int k = 0 ; k < 7; k++) {
if (j==set [k]) {
unique = false;
break;
}
}
if (unique) {
set [i] = j;
}
}
}
for (int i= 0; i < 6; i++) {
if (i == 5) {
System.out.println(set[i]);
}
else {
System.out.print(set[i]+", ");
}
}
System.out.println("Bonus Ball = " + set[6]);
}
}
I was wondering how I would implement bubble-sort into this code. It works on my machine and produces 6 random numbers + a bonus ball.
2, 34, 25, 14, 39, 13
Bonus Ball = 30
I was aiming to make it print so the numbers are ascending so that it would be like the lotto, the idea anyway.
That's all, thanks.
Can make like this:
for (int i = set.length-1; i >= 0; i --) {
for (int j = 0; j < i; j++) {
if (set[j] > set[j + 1]) {
int aux = set[j];
set[j] = set[j + 1];
set[j + 1] = aux;
}
}
}
The initial loop, in this way also helps, not to waste resources comparing indexes that have already been sorted.
If the use of bubble sort algorithm is not mandatory, just use the Array.sort method already mentioned.
for (i = 0 ; i < set.length() -2; i ++){
for(j = i+1 ; j< set.length()-1 ; j++){
if(set[i]<set[j]){
int dummy = set[i];
set[i] = set[j];
set[j] = dummy;
}
}
}
The above bubble sort is sorting only the 6 generated numbers and your Bonus ball is still present at set[6]
So now rest is your printing function works.
Bubble sort set array :-
int temp = 0;
for(int i = 0;i<set.length-1;i++)
{
for(int j = 0;j<set.length-1;j++)
{
if(set[j]>set[j+1])
{
temp = set[j];
set[j] = set[j+1];
set[j+1] = temp;
}
}
}
//Your set array is sorted using bubble sort at this point.

Finding multiple modes in an array of integers with 1000 elements

So I need a way to find the mode(s) in an array of 1000 elements, with each element generated randomly using math.Random() from 0-300.
int[] nums = new int[1000];
for(int counter = 0; counter < nums.length; counter++)
nums[counter] = (int)(Math.random()*300);
int maxKey = 0;
int maxCounts = 0;
sortData(array);
int[] counts = new int[301];
for (int i = 0; i < array.length; i++)
{
counts[array[i]]++;
if (maxCounts < counts[array[i]])
{
maxCounts = counts[array[i]];
maxKey = array[i];
}
}
This is my current method, and it gives me the most occurring number, but if it turns out that something else occurred the same amount of times, it only outputs one number and ignore the rest.
WE ARE NOT ALLOWED TO USE ARRAYLIST or HASHMAP (teacher forbade it)
Please help me on how I can modify this code to generate an output of array that contains all the modes in the random array.
Thank you guys!
EDIT:
Thanks to you guys, I got it:
private static String calcMode(int[] array)
{
int[] counts = new int[array.length];
for (int i = 0; i < array.length; i++) {
counts[array[i]]++;
}
int max = counts[0];
for (int counter = 1; counter < counts.length; counter++) {
if (counts[counter] > max) {
max = counts[counter];
}
}
int[] modes = new int[array.length];
int j = 0;
for (int i = 0; i < counts.length; i++) {
if (counts[i] == max)
modes[j++] = array[i];
}
toString(modes);
return "";
}
public static void toString(int[] array)
{
System.out.print("{");
for(int element: array)
{
if(element > 0)
System.out.print(element + " ");
}
System.out.print("}");
}
Look at this, not full tested. But I think it implements what #ajb said:
private static int[] computeModes(int[] array)
{
int[] counts = new int[array.length];
for (int i = 0; i < array.length; i++) {
counts[array[i]]++;
}
int max = counts[0];
for (int counter = 1; counter < counts.length; counter++) {
if (counts[counter] > max) {
max = counts[counter];
}
}
int[] modes = new int[array.length];
int j = 0;
for (int i = 0; i < counts.length; i++) {
if (counts[i] == max)
modes[j++] = array[i];
}
return modes;
}
This will return an array int[] with the modes. It will contain a lot of 0s, because the result array (modes[]) has to be initialized with the same length of the array passed. Since it is possible that every element appears just one time.
When calling it at the main method:
public static void main(String args[])
{
int[] nums = new int[300];
for (int counter = 0; counter < nums.length; counter++)
nums[counter] = (int) (Math.random() * 300);
int[] modes = computeModes(nums);
for (int i : modes)
if (i != 0) // Discard 0's
System.out.println(i);
}
Your first approach is promising, you can expand it as follows:
for (int i = 0; i < array.length; i++)
{
counts[array[i]]++;
if (maxCounts < counts[array[i]])
{
maxCounts = counts[array[i]];
maxKey = array[i];
}
}
// Now counts holds the number of occurrences of any number x in counts[x]
// We want to find all modes: all x such that counts[x] == maxCounts
// First, we have to determine how many modes there are
int nModes = 0;
for (int i = 0; i < counts.length; i++)
{
// increase nModes if counts[i] == maxCounts
}
// Now we can create an array that has an entry for every mode:
int[] result = new int[nModes];
// And then fill it with all modes, e.g:
int modeCounter = 0;
for (int i = 0; i < counts.length; i++)
{
// if this is a mode, set result[modeCounter] = i and increase modeCounter
}
return result;
THIS USES AN ARRAYLIST but I thought I should answer this question anyways so that maybe you can use my thought process and remove the ArrayList usage yourself. That, and this could help another viewer.
Here's something that I came up with. I don't really have an explanation for it, but I might as well share my progress:
Method to take in an int array, and return that array with no duplicates ints:
public static int[] noDups(int[] myArray)
{
// create an Integer list for adding the unique numbers to
List<Integer> list = new ArrayList<Integer>();
list.add(myArray[0]); // first number in array will always be first
// number in list (loop starts at second number)
for (int i = 1; i < myArray.length; i++)
{
// if number in array after current number in array is different
if (myArray[i] != myArray[i - 1])
list.add(myArray[i]); // add it to the list
}
int[] returnArr = new int[list.size()]; // create the final return array
int count = 0;
for (int x : list) // for every Integer in the list of unique numbers
{
returnArr[count] = list.get(count); // add the list value to the array
count++; // move to the next element in the list and array
}
return returnArr; // return the ordered, unique array
}
Method to find the mode:
public static String findMode(int[] intSet)
{
Arrays.sort(intSet); // needs to be sorted
int[] noDupSet = noDups(intSet);
int[] modePositions = new int[noDupSet.length];
String modes = "modes: no modes."; boolean isMode = false;
int pos = 0;
for (int i = 0; i < intSet.length-1; i++)
{
if (intSet[i] != intSet[i + 1]) {
modePositions[pos]++;
pos++;
}
else {
modePositions[pos]++;
}
}
modePositions[pos]++;
for (int modeNum = 0; modeNum < modePositions.length; modeNum++)
{
if (modePositions[modeNum] > 1 && modePositions[modeNum] != intSet.length)
isMode = true;
}
List<Integer> MODES = new ArrayList<Integer>();
int maxModePos = 0;
if (isMode) {
for (int i = 0; i< modePositions.length;i++)
{
if (modePositions[maxModePos] < modePositions[i]) {
maxModePos = i;
}
}
MODES.add(maxModePos);
for (int i = 0; i < modePositions.length;i++)
{
if (modePositions[i] == modePositions[maxModePos] && i != maxModePos)
MODES.add(i);
}
// THIS LIMITS THERE TO BE ONLY TWO MODES
// TAKE THIS IF STATEMENT OUT IF YOU WANT MORE
if (MODES.size() > 2) {
modes = "modes: no modes.";
}
else {
modes = "mode(s): ";
for (int m : MODES)
{
modes += noDupSet[m] + ", ";
}
}
}
return modes.substring(0,modes.length() - 2);
}
Testing the methods:
public static void main(String args[])
{
int[] set = {4, 4, 5, 4, 3, 3, 3};
int[] set2 = {4, 4, 5, 4, 3, 3};
System.out.println(findMode(set)); // mode(s): 3, 4
System.out.println(findMode(set2)); // mode(s): 4
}
There is a logic error in the last part of constructing the modes array. The original code reads modes[j++] = array[i];. Instead, it should be modes[j++] = i. In other words, we need to add that number to the modes whose occurrence count is equal to the maximum occurrence count

Categories

Resources