lets say i have this array
int [] array= new int[26];
it has 26 places because the position 0 is 'a' , position 1'b' ... position 25 is 'z'
so in each position i have an int number so
if in position array[0]=5 it means i have 5 'a'
if in position array[1]=6 it means i have 6'b'
if in position array[0]=0 it means that i do not have the 'a' letter
what i want is to find in each loop the 2 smallest frequencies and the letters of the two smallest frequencies
for(int i=0;i<array.length;i++)
if(array[i]==0)
continue;
else{
cmin1=(char)('a'+i);
posi=i;
min1=array[posi] ;
break;
}
for(int j=posi+1;j<array.length;j++){
if(array[j]==0)
continue;
else if(array[j]<=min1){
posj=posi;
posi=j;
cmin2=cmin1;
cmin1=(char)(j+'a');
min2=min1;
min1=array[j];
}
i have tried this which is wrong
Java is Object Oriented so...
Let's take a class, which name would be LetterFrequency
LetterFrequency has 2 attributes:
1) Char character
2) Integer occurrences
You need to sort the LetterFrequency objects by their "occurrences" atrribute. To do that, make LetterFrequancy implements Comparable and define method compareTo() accordingly.
Then put all your LetterFrequency objects in a List and use the method
Lists.sort(yourList)
Sort would work but it's not the best performing method.
How about a single loop which would be O(n)?
int min1 = Integer.MAX_INT;
int idx1 = -1;
int min2 = Integer.MAX_INT;
int idx2 = -1;
for(int i=0;i<array.length;i++) {
// skip empty items
if(array[i]==0)
continue;
if (array[i] < min1) {
min2 = min1;
idx2 = idx1;
min1 = array[i];
idx1 = i;
}
else if (array[i] < min2) {
min2 = array[i];
idx2 = i;
}
}
sort the array first...now apply the searching algorithm...and here you go
once you find the smallest element you can get the second smallest as the array is already sorted...i guess there would be no difficulty doing this...for sorting you can use quicksort with complexity(nlogn)...hope it helps you
I would create a class representing each frequency count. Then I would create a Comparator which orders the records by the frequency. Then I would use Arrays.sort() or Collections.sort() to sort the collection using the Comparator.
Alternatively, if you simply want to find the entries in the array but can't change your data type, then you need to define an algorithm for searching (not sorting) the array. To do this, and do it in one pass, I would define local variables for the positions and corresponding frequencies of the two least frequently occurring. Initialize the least to the first item in the array, then proceed by comparing the current one and if it is less then rotate the values of the variables which are keeping track. At the end you'll have the two least frequent.
If you just want to find the smallest element in an array, you can use the following code :
List<int> list = Arrays.asList(ArrayUtils.toObject(array));
// Print the smallest element of your array
System.out.println(Collections.min(list));
First of all, this array declaration will never work:
int [] array= new array[26];
You need:
int [] array= new int[26];
Related
I am creating a code here but I believe there is a way making the following more efficient. I tried many ways but it does not seem to work.
protected void randomise() {
int[] copy = new int[array().length]; //This makes the new int called randomIndex
// used to indicate if elements have been used
boolean[] used = new boolean[array().length]; //calling "used" as a new boolean for the array
Arrays.fill(used,false);
/**
* if index = 0, it means their is nothing in the index.
* if you apply a random number, it will copy that number to an array called index.
* if randomIndex in use, then the boolean becomes true.
*/
for (int index = 0; index < array().length;) {
do {
randomIndex = randomIndex();
} while (used[randomIndex]); //when random is in use, do the follow instruction.
copy[index] = array[index]; //copy the value value to an array called index.
used[randomIndex] = true; //when randomIndex is in use, then it becomes true.
}
//Of course, if there an extra random stores in the array, the index list is increased by one (index++).
for (int index =0;index < array().length; index++) {
array()[index] = copy[index]; //This tells where to copy the index value. in this case, it is a index array.
}
Do you have to use randomIndex?
If not you can use your bool[] to eliminate that do {} while() by sequentially adding the value to copy (which isn't a great name) and choosing a randInt in the range of the len of elements that haven't been selected, then using that bool[] to count a walk through the array elements ( to make your choice for the next element in copy.
You seem to want to randomly re-order an array. If so, then indeed there is a much more efficient solution. You are currently keeping two extra arrays on the size of the input (O(n)) while you do not have to.
The random shuffling is a common problem, and obviously there have been proposed several algorithms to accomplish this task. One of the most efficient algorithms is Knuth's algorithm for random permutation
The algorithms idea is, loop over the array once, and for each number i, perform a random exchange between i and a (random) array index between 0 and i. This guarantees that the array with be shuffled (meaning that each item will have equal possibility to be placed in each of the array indexes), in O(n) time and without using any extra space.
In short,
for (int i = 0; i < index; i++) {
int r = random.nextInt(i + 1);
exchange(array, i, r);
}
It is simple - use some collection of indexes and remove element when you used it. This way should looks like:
List<Integer> indexes = new ArrayList<>(array.length);
for (int i = 0 ; i < array.length ; i++) {
indexes.add(i);
}
Random r = new Random();
while (indexes.size() > 0) {
int randomIndex = r.nextInt(indexes.size());
int index = indexes.remove(randomIndex);
copy[index] = array[index];
}
Please note that:
you should check what is exact collection will be more efficient in your situation
Another way - create list values for array and use Collections.shuffle method on this list.
Additional another way - use some recursive algorithm to do that work.
I have a question that doesn't seem possible to me. I have 2+ arrays which I have to compare for common values. I am supposed to do this in O(N) comparisons but can't think of a way. Specifically (k-1)N comparisons where k is the number of arrays. I've narrowed down how I can take multiple arrays and just merge them into a single array. I've gotten that the smallest array is the limiting factor so if I sort that I can save the most comparisons. After spending half the day staring at my screen I've even come up with a way to do this linearly if I discount any duplicates, but I have to keep duplicates So, as far as I know in order to compare any arrays you need at least 2 for loops which would be O(N^2) wouldn't it? I'm also not allowed to hash anything.
For example if I had {1,3,4,3,4,5} as a master and {1,1,3,5,9,3,7} and {3,5,8,4,0,3,2} as arrays to be compared I'd need to have a final of {3,3,5} since I can't get rid of any dupiclates.
I don't want anyone to write the code, I just need to know what I should be doing in the first place.
Use an array of ints. Taking your first list, for each element, set the value at that index to 1. So if the first element is 3, put 1 in array[3]. Now, we know that 3 is present in first list. Putting 1 will help you distinguish from a 3 that is present in the earlier list versus a 3 which is repeated in current list.
Iterate through all the other k-1 lists
For every element, check the value in array for that index
If the value is 0, set it to this list number
If the value is a number less than this list number, this number is a duplicate and has already appeared in a previous list.
If this number is equal to this list index it means this number already occurred in this list but not in previous lists, so not yet a duplicate.
The numbers that you are getting as duplicates, add them to another list.
Finish all iterations
Finally print the duplicates.
Original Wrong Answer
Create a HashSet<int>
Take all values from master and add to it - O(master list count)
Now just iterate through first and second arrays and see if their elements are in that HashSet - O(each list count)
If the lists are sorted, then it's relatively straightforward if you do something like this:
List<Integer> intersection = new ArrayList<>();
int i = 0;
int j = 0;
while (i < list1.size() && j < list2.size()) {
int a = list1.get(i);
int b = list2.get(j);
if (a < b) {
i++;
} else if (b < a) {
j++;
} else { // a == b
intersection.add(a);
i++;
j++;
}
}
On each iteration of the loop, the quantity i + j increases by at least 1, and the loop is guaranteed to be done when i + j >= list1.size() + list2.size(), so the whole thing does at most O(list1.size() + list2.size()) comparisons.
I have an array of N elements and contain 1 to (N-1) integers-a sequence of integers starting from 1 to the max number N-1-, meaning that there is only one number is repeated, and I want to write an algorithm that return this repeated element, I have found a solution but it only could work if the array is sorted, which is may not be the case.
?
int i=0;
while(i<A[i])
{
i++
}
int rep = A[i];
I do not know why RC removed his comment but his idea was good.
With the knowledge of N you easy can calculate that the sum of [1:N-1]. then sum up all elementes in your array and subtract the above sum and you have your number.
This comes at the cost of O(n) and is not beatable.
However this only works with the preconditions you mentioned.
A more generic approach would be to sort the array and then simply walk through it. This would be O(n log(n)) and still better than your O(n²).
I you know the maximum number you may create a lookup table and init it with all zeros, walk through the array and check for one and mark the entries with one. The complexity is also just O(n) but at the expense of memory.
if the value range is unknown a simiar approach can be used but instead of using a lookup table a hashset canbe used.
Linear search will help you with complexity O(n):
final int n = ...;
final int a[] = createInput(n); // Expect each a[i] < n && a[i] >= 0
final int b[] = new int[n];
for (int i = 0; i < n; i++)
b[i]++;
for (int i = 0; i < n; i++)
if (b[i] >= 2)
return a[i];
throw new IllegalArgumentException("No duplicates found");
A possible solution is to sum all elements in the array and then to compute the sym of the integers up to N-1. After that subtract the two values and voila - you found your number. This is the solution proposed by vlad_tepesch and it is good, but has a drawback - you may overflow the integer type. To avoid this you can use 64 bit integer.
However I want to propose a slight modification - compute the xor sum of the integers up to N-1(that is compute 1^2^3^...(N-1)) and compute the xor sum of your array(i.e. a0^a1^...aN-1). After that xor the two values and the result will be the repeated element.
This question already has answers here:
Sort an array in Java
(19 answers)
Closed 9 years ago.
Im trying to organize random numbers in an array from least to greatest.
I came up with a loop which I thought should work but has a lot of logic errors.
for(int z=0; z<=999;z++){
for(w=1; w<=999;w++){
if(z<w){
if(numberArray[z]<numberArray[w])
temp=numberArray[w];
}
}
numberArray[z]=temp;
}
Can anyone tell me how to fix this or an algorithm of their own for doing this?
There are several ways you can sort an array in Java. Here I post but 3 of them : the core library, and 2 algorithms you can make on your own.
1 ) Core one: This is literally only one line of code. I would suggest using this - simple, and very efficient, compared to the below two solutions.
Arrays.sort(myArray);
2 ) Selection Sort : Find the lowest value in an array, move it to the first position, find the next lowest, move to 2nd position, etc.
public void selectionSort(Comparable[] a)
{
for(int index = 0; index < a.length; index++)
{
// find the smallest one in the array from index : end
int smallest = indexOfMin(a, index);
// swap the value at index and the value at the smallest one found
Comparable temp = a[smallest];
a[smallest] = a[index];
display.update();
a[index] = temp;
}
}
3 ) Insertion Sort : Inserts each element in the array into a growing sequence of sorted values and finishes at the end of the array.
public void insertionSort(Comparable[] a)
{
for(int i = 1; i < a.length; i++)
{
insert(a, i);
}
}
public void insert(Comparable[] a, int nextIndex)
{
int index = 0;
Comparable finalObject = a[nextIndex];
// Let us first find the first occurence of a comparable greater than our comparable
while(finalObject.compareTo(a[index]) > 0)
index++;
for(int i = (nextIndex-1); i >= index; i--)
a[i+1] = a[i];
a[index] = finalObject;
}
One liner:
Arrays.sort(numberArray);
Or greatest to least order:
Arrays.sort(numberArray, Collections.reverseOrder());
Or even better, use a Binary Search Tree that keeps its contents in sorted order, this is great for collections that are pretty dynamic, as the add operation is cheaper memory wise and time wise than a full in-place sort:
TreeSet<int> set = new TreeSet<int>();
set.add(10);
set.add(4);
set.add(11);
set.toString();
// prints 4, 10, 11
Arrays.sort() is a quick and easy way.
Also consider PriorityQueues if you need something a little more robust!
This link is another question on SO with a great answer.
Heap - Sort Algorithm
The problem I am having is this, this algorithms n input is 2, this is designed so that the 1st position (int i) of the array and the 2nd position (int j) have their values compared.
The problem is that this ignores the 0 position of the given array list. I have tried reducing certain values, this will create infinite loops. The algorithm is an adaptation of pseudocode. It isn't designed to run arraylist from 0. I can't think of how to re-adapt this algorithm into a decent minimum heap sort.
public static void input( ArrayList<input> vertexList, int n )
{
int j=n;
int i=n/2;
input object = vertexList.get(n);
while ((i>0) && vertexList.get(i)> object){
vertexList.set(j, vertexList.get(i));
j = i;
i = i/2;
}
vertexList.set(j, object);
}
try to use vertexList.get(i-1) and vertexList.get(j-1) and vertexList.set(j-1, ...)