Java: Check if exists three or more matches in array
I wish to make a function that checks whether theres any number in the array that exist three or more times.
So example this array:
4 - 6 - 14 - 8 - 6 - 15 - 14 - 15 - 13 - 10 -
Shouldnt output anything, but this:
4 - 6 - 14 - 8 - 6 - 15 - 14 - 15 - 14 - 10 -
Should systemout print output that the number 14 exists three times.
How should this be done? I started making a for loop,
开发者_如何学运维for(int i=0; i<array.length; i++){
}
And then I got stuck, how can i make this?
for (int i = 0; i < array.length; i++) {
int count = 0;
for (int j = 0; j < array.length; j++) {
{
if (array[i] == array[j]) {
count++;
}
}
if (count >= 3) {
System.out.println(array[i] + " exists " + count
+ " times.");
}
}
}
This programm outputs n times (for n >= 3) that the an element exists n times. You should add a logic that checks whether an element has already been checked. But maybe you can go on by yourself now. Hint: Use a HashMap or a List... or....
Other solution that needs Integer[]
instead of int[]
and is faster:
Integer[] array = new Integer[] { 1, 2, 1, 1, 1, 3, 3, 3, 2, 2, 4, 5 };
for (int i = 0; i < array.length; i++) {
int count = 0;
if (array[i] != null) {
int compare = array[i].intValue();
for (int j = 0; j < array.length; j++) {
if (array[j] != null) {
if (compare == array[j]) {
array[j] = null;
count++;
}
}
}
if (count >= 3) {
System.out
.println(compare + " exists " + count + " times.");
}
}
}
Output:
1 exists 4 times.
2 exists 3 times.
3 exists 3 times.
So copy your array into a Integer[] array first. The latter algorithm fixes also the problem with the output.
simply make a hashmap
of numbers with numbers value as key and the occurrence times as map value.
after the for loop you can iterate over the map and print the numbers whose occurrence value is more than 3
you can sort the array and then check for each new element if the next two elements are the same. if they do - save the element. if they don't reset the counter and go to the next element
Here is a solution for the general case: Search in a list for every Element whether there are X occurences following.
public class TripleTest
{
public static void main (String[] args)
{
int[] list = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
findCount (list, 3);
}
public static void findCount (int[] list, int count)
{
for (int i = 0; i <= list.length - count; ++i)
{
int elem = list [i];
boolean hit = findWhatCountFrom (list, i + 1, elem, count - 1);
if (hit)
System.out.println ("hit: " + elem);
}
}
public static boolean findWhatCountFrom (int[] list, int idx, int what, int count)
{
if (count == 0)
return true;
if (idx == list.length)
return false;
return findWhatCountFrom (list, idx + 1, what, count - ((list [idx] == what) ? 1 : 0));
}
}
It doesn't stop after one solution, so maybe you like to change that. The requierements wheren't clear. Instead of a return value, the solution is printed out - that's a bit ugly.
Improve the solution by changing it from int[] to Generics. Maybe from Array to List.
You could start with sorting the array, then start checking the i with i+1, if are equal then you increment counter otherwise you set him to 0, you repeat this until counter is not equal 2.
private static int findTree(int[] array){
Arrays.sort(array);
int i = 0;
int counter = 0;
while (counter != 2 && i < array.length - 1) {
if(array[i] == array[i+1]){
counter++;
} else {
counter =0;
}
i++;
}
if(counter == 2){
return array[i];
}
return -1;
}
EDIT:
More tricky alg, that find n matches of given number.
The assumption is that you have to find at lest n equal numbers.
If we sort our array first, we will have all elements in order, so if we need to find four (n) equal elements we can take one (i) and check the element that is distanced three spaces from him (i+n-1). If this element is equal we found our solution under i element in array if not we can move to next one by incrementing i by one (i++).
And this is how it goes:
private static int findN(int[] array, int n){
if(n == 0) {
return -1;
}
Arrays.sort(array);
n--; //We reduce our n searched element because in i we already have one
int i = 0;
boolean found = false;
while (found == false && i < array.length - n) {
if(array[i] == array[i+n]) { //remember that we have reduced the n by one.
found = true;
} else {
i++;
}
}
if(found) {
return array[i];
}
return -1;
}
What if a number exists 4 times?
- No output, because 3 is not 4, you want exactly 3
- 1x output, because you search for at least 3 hits
- 2x output, because you can show 3 triples
What if more than one number exists 3 or more times?
- Report every number
- Report the first, only
精彩评论