Home > OS >  How can I stop the repetition of a value that has been already been checked in a function?
How can I stop the repetition of a value that has been already been checked in a function?

Time:01-04

int [] firstArr = Utils.arrGen(len, min, max);
int [] secondArr = Utils.arrGen(len, min, max);
for (int i = 0; i < firstArr.length; i  ) {
   int [] result = Utils.arrRepeatCheck(secondArr, firstArr[i]);
   if (result[1] != 0) {
      System.out.println("The number "   result[0]   " was found to be repetetive in the arrays "   result[1]   " times!");
   }
}
public static int [] arrRepeatCheck(int [] arr, int num) {
   int counter = 0;
   for (int i = 0; i < arr.length; i  ) {
      if (arr[i] == num) {
         counter  ;
      }
   }


int [] result = {num, counter};
return result;

(The function Utils.arrGen generates arrays with random values in them in a certain range, btw arrRepeartCheck is already in the utils class.)

I tried using an if statement to check if the number has been already tested, but it failed.

The number 1 was found to be repetetive in the arrays 12 times!
The number 3 was found to be repetetive in the arrays 15 times!
The number 1 was found to be repetetive in the arrays 12 times!
The number 1 was found to be repetetive in the arrays 12 times!
The number 2 was found to be repetetive in the arrays 8 times!
The number 0 was found to be repetetive in the arrays 15 times!
The number 1 was found to be repetetive in the arrays 12 times!
The number 0 was found to be repetetive in the arrays 15 times!
The number 2 was found to be repetetive in the arrays 8 times!
The number 0 was found to be repetetive in the arrays 15 times!
The number 2 was found to be repetetive in the arrays 8 times!
The number 1 was found to be repetetive in the arrays 12 times!
The number 3 was found to be repetetive in the arrays 15 times!
The number 2 was found to be repetetive in the arrays 8 times!
The number 3 was found to be repetetive in the arrays 15 times!
The number 3 was found to be repetetive in the arrays 15 times!
The number 1 was found to be repetetive in the arrays 12 times!
The number 3 was found to be repetetive in the arrays 15 times!
The number 0 was found to be repetetive in the arrays 15 times!
The number 3 was found to be repetetive in the arrays 15 times!
The number 3 was found to be repetetive in the arrays 15 times!
The number 0 was found to be repetetive in the arrays 15 times!
The number 3 was found to be repetetive in the arrays 15 times!
The number 2 was found to be repetetive in the arrays 8 times!
The number 1 was found to be repetetive in the arrays 12 times!
The number 3 was found to be repetetive in the arrays 15 times!
The number 0 was found to be repetetive in the arrays 15 times!
The number 3 was found to be repetetive in the arrays 15 times!

Process finished with exit code 0

CodePudding user response:

Convert the array to a set before checking its values. This will remove all duplicates.

Try something like this:

var mySet = IntStream.of(firstArr).boxed().collect(Collectors.toSet());
for (int i = 0; i < mySet.size(); i  ) {
  int [] result = arrRepeatCheck(secondArr, (Integer) mySet.toArray()[i]);
  if (result[1] != 0) {
    System.out.println("The number "   result[0]   " was found to be repetetive in the arrays "   result[1]   " times!");
  }
}

CodePudding user response:

I think the optimal solution for you (using a static array) would be:

public static int [] arrRepeatCheck(int [] arr, int num) {

int counter = 0, value_modified = -1;

for (int i = 0; i < arr.length; i  ) {
   if (arr[i] == num && arr[i] != value_modified) {
      arr[i] = value_modified;
      counter  ;
   }
}
...

When the value_modified has a unique value with the elements of the array.

  • Related