COMPARE DATAS to know if at least one data has the same value as another

Dear you :wink:

I’m trying to find a way to compare two datas or more, in order to know if they has the same value.
For exemple if the data TrigmodPos[10]=0 && TrigmodPos[7]=0, I would like to switch only theses datas as TrigmodPos[10]=1, TrigmodPos[7]=1

Other exemple if TrigmodPos[10]=0 && TrigmodPos[9]=0 && TrigmodPos[8]=0 && TrigmodPos[7]=0 then TrigmodPos[10]=1, TrigmodPos[9]=1 , TrigmodPos[8]=1, TrigmodPos[7]=1

Thank for helping :kissing_heart:

Same values or if they’re all 0 only?

2 Likes

If they have the value==0 only, indeed.

And if they’re all 0, they’re all set to 1, is it?

Yes exactly what i would like :ok_hand:

  • My approach for it is to sum all values of the selected indices and check if the result is still 0.
  • If that’s true, repeat the same loop to set all selected indices to 1.
  • There are 2 overloaded versions for ifAll0SetTo1(). 1 for byte[] and the other for int[]:
/**
 * Set 1 If All Indices Are 0 (v1.0.6)
 * GoToLoop (2022/Dec/12)
 *
 * https://Discourse.Processing.org/t/logical-test-to-know-if-at-least-
 * one-data-has-the-same-value-as-another/40133/6
 */

final byte[] trigModPos = new byte[5];

void setup() {
  println(trigModPos);
  println(ifAll0SetTo1(trigModPos, 4, 0, 2)); // true
  println(ifAll0SetTo1(trigModPos, new int[] { 3, 0 })); // false
  println(trigModPos);
  exit();
}

@SafeVarargs static final boolean
  ifAll0SetTo1(final byte[] arr, final int... indices)
{
  assert arr.length * indices.length != 0: "Array argument is empty";

  final boolean allZero;
  int sum = 0;

  for (final int idx : indices)  sum += arr[idx];

  if (allZero = sum == 0)  for (final int idx : indices)  arr[idx] = 1;

  return allZero;
}

@SafeVarargs static final boolean
  ifAll0SetTo1(final int[] arr, final int... indices)
{
  assert arr.length * indices.length != 0: "Array argument is empty";

  final boolean allZero;
  int sum = 0;

  for (final int idx : indices)  sum += arr[idx];

  if (allZero = sum == 0)  for (final int idx : indices)  arr[idx] = 1;

  return allZero;
}
1 Like

I am not familiarized at all with a lot of term in your program. :face_in_clouds:
Here what I did

int [] TrigmodPos = new int [12];
boolean [] boolTrigmodPos =  new boolean [12];

void setup () {  

TrigmodPos[11]=1;
TrigmodPos[10]=0;
TrigmodPos[9]=1;
TrigmodPos[8]=0;
TrigmodPos[7]=1;
TrigmodPos[6]=0;
TrigmodPos[5]=1;
TrigmodPos[4]=0;

 } 

void draw () { 

for ( int i=0; i<12; i++ ) { 
  if (TrigmodPos[i]<1) { 
  boolTrigmodPos[i] = true;
 
  }
  else  if (TrigmodPos[i]>0) {  boolTrigmodPos[i] = false;
   }
   println ( " TrigmodPos ", i, " ", boolTrigmodPos[i] );   
 }   
 noLoop();
 } 

Could you detail, or showing example please?

I have tried this to compare all data but only in 3 arrangement.
But I think it should be better to compare data in an array but I understand nothing!


int [] TrigmodPos = new int [12];
boolean [] boolTrigmodPos =  new boolean [12];
boolean [] originalBoolTrigmodPos =  new boolean [12];

void setup () {  
TrigmodPos[11]=1;
TrigmodPos[10]=0;
TrigmodPos[9]=1;
TrigmodPos[8]=0;
TrigmodPos[7]=1;
TrigmodPos[6]=0;
TrigmodPos[5]=1;
TrigmodPos[4]=0;
TrigmodPos[3]=0;
TrigmodPos[2]=0;
 } 
void draw () { 
for ( int i=11; i>=2; i-- ) { 
  if (TrigmodPos[i]>0) { 
  originalBoolTrigmodPos[i] = true;
  }
  else originalBoolTrigmodPos[i] = false;
  println
  ( " SET ", i, " ", originalBoolTrigmodPos[i] );   
 } 
 
 
 for ( int j = 10 ; j>=2; j-- ){ 
   
   int f = 11;
 for ( int k = 9 ; k>=2; k-- ){  

 
 if ( originalBoolTrigmodPos[f] == true  &&  originalBoolTrigmodPos[j] == true   )  { 
      boolTrigmodPos[f]= false; boolTrigmodPos[j]= false;
 } 
 else 
 boolTrigmodPos[f]=originalBoolTrigmodPos[f];
 boolTrigmodPos[j]=originalBoolTrigmodPos[j];
 
  println (" FIR ", f , " ", boolTrigmodPos[f],  " boolTrigmodPos ", j , " ", boolTrigmodPos[j] );  
 
 // second test 
  
  
 if (( originalBoolTrigmodPos[f] == true  &&  originalBoolTrigmodPos[j] == true  &&  originalBoolTrigmodPos[k] == true  ) && (j!=k))  { 
      boolTrigmodPos[f]= false; boolTrigmodPos[j]= false; boolTrigmodPos[k]= false;
 } 
 /*
 else  if ( originalBoolTrigmodPos[f] == true  &&  originalBoolTrigmodPos[j] == true   )  { 
      boolTrigmodPos[f]= false; boolTrigmodPos[j]= false;
 } 

 */
 
 
  println (" SEC ", f , " ", boolTrigmodPos[f],  " boolTrigmodPos ", j , " ", boolTrigmodPos[j] , " boolTrigmodPos ", k , " ", boolTrigmodPos[k] );  
  
  /*  
   int k;  
  k= (j-1);
  if (k< 2){
  k= 1;
  */
  } 
}
  
  

 noLoop();
}
  • The code inside callback setup() is the example.
  • While ifAll0SetTo1() is the implementation.
  • Function ifAll0SetTo1() expects an int[] or byte[] as the array to be checked.
  • Followed by either an int[] array or a sequence of value arguments as the indices to be checked.
  • If all checked indices contain value 0, change those to 1 and return true.
  • Otherwise return false and the passed array is unmodified.
  • Given the array argument is subject to be modified, if you wanna keep the original, pass a clone() of it instead.
  • BtW, you can convert an int[] to a boolean[] array via boolean().
  • Here’s another setup() example.
  • Just re-use function ifAll0SetTo1() from my original sketch:
/**
 * Set 1 If All Indices Are 0 (v1.1.1)
 * GoToLoop (2022/Dec/12)
 *
 * https://Discourse.Processing.org/t/logical-test-to-know-if-at-least-
 * one-data-has-the-same-value-as-another/40133/10
 */

final int[]
  original = { 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1 }, 
  cloned = original.clone();

boolean[] bools;

void setup() {
  println(ifAll0SetTo1(cloned, 10, 1, 6)); // true
  println(ifAll0SetTo1(cloned, new int[] { 11, 8 })); // false

  println(str(original));
  println(str(cloned));

  bools = boolean(cloned);
  println(str(bools));

  exit();
}

Hi,
I’m really trying to understand what you said, it doesn’t sound complicated, but I haven’t learned the processing with terms like arguments and clues. The argument is 0 or 1 and the indices are the data to be tested in the array.
Could you make at least half of the program work so I can understand better please?

If I understand the problem correctly then we have

  1. a working array where we want to compare elements
  2. a list of elements that must all match a certain value (zero)
  3. if step 2 is successful change these elements to a new value (one)

To prevent runtime errors it would also be useful to make sure we only look at elements that exist in the working array. We can do this by checking the index values in our element list.

If I am correct then the sketch demonstrates a method that does all this :smile:

int[] original = { 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1 };
int[] me, result;

void setup() {
  println("Original");
  showArray(original);
  // These tests used a copy of the original array so that we can perform multiple
  // test using the same working array
  println("Test elements 0, 3, 4, 10    PASS elements match");
  me = new int[] {0, 3, 4, 10};
  result = matchData(0, 1, me, original.clone());
  showArray(result);

  println("Test elements 0, 1, 6, 8, 15    FAIL non existant element (15)");
  me = new int[] {0, 1, 6, 8, 15};
  result = matchData(0, 1, me, original.clone());
  showArray(result);
}

/*
This is the main function
 matchValue     : the value all the elements must match
 newValue       : if all the required elements macth the required value change
                  them to this value
 matchElements  : an array of the elements to be tested
 theArray       : the array we want to test
 
 This function will test all the elements in theArray listed in the matchElements
 array. If they are all valid elements in theArray and they equal the matchValue
 the elements will be changed to the new value.
 */
int[] matchData(int matchValue, int newValue, int[] matchElements, int[] theArray) {
  boolean matched = true; //  assume they are going to match
  for (int i = 0; i <  matchElements.length; i++) {
    int e = matchElements[i];
    matched &= (e >=0 && e < theArray.length && theArray[e] == matchValue);
    if (!matched) break; // element does not exist or does not match required value
  }
  if (matched) {
    for (int i = 0; i <  matchElements.length; i++)
      theArray[matchElements[i]] = newValue;
  }
  return theArray;
}

void showArray(int[] array) {
  for (int i = 0; i < array.length; i++)
    print(array[i] + "   ");
  println();
}

The method is flexible in that it can

  • use any match value
  • replace with any new value
  • can compare any number of elements

The program is compilable, runnable and outputs the test results to the console.

As I’ve warned, you need to copy & paste function ifAll0SetTo1() from the original sketch as a requirement for this new setup() test.

Argument is the value passed to a function’s parameter:

In short, the values we pass to a function is called arguments.
And those arguments initialize the parameters of the invoked function.

This is 1 of the ifAll0SetTo1()'s signatures in a more simplified form:
boolean ifAll0SetTo1(int arr, int… indices)

  • Keyword boolean indicates the function must return either a true or false value.
  • Value true means success. That is, all supplied indices contain value 0 & they’ve been replaced by value 1 inside the function.
  • 1st parameter (int[] arr) represents the array we wanna check its indices for value 0.
  • 2nd parameter (int... indices) represents the indices to be checked for value 0 from the 1st parameter array.
  • Notice the datatype for the 2nd parameter is int....
  • Datatype int... is equivalent to int[]'s, but w/ an extra special feature:
  • We can pass either a single int[] array argument to it or multiple int non-array arguments!
  • For the latter case, those non-array arguments are converted to a single array!
  • That special parameter is called variadic arguments (a.K.a. varargs).

P.S.: When you don’t understand anything from a code, mention it when asking a question rather than just saying:

So we can know precisely which parts we need to clarify in detail.

2 Likes

Hi quarks,

I think I asked my request in a bad manner. I 'm totally sorry.
I wanted to compare all datas, if one of them is 0, the value is 0. If at least two of them are 0, these 2 values become 1.

I made changes in your program with tow different cases

int[] original = { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int[] original2 = { 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 };

int[] me, result;

void setup() {
  println("Original");
  showArray(original);

  // These tests used a copy of the original array so that we can perform multiple
  // test using the same working array


  println("Test All elements from Original 1   ");
  me = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  result = matchData(0, 1, me, original.clone());
  showArray(result);
  
  
  
  println("Original2");
  showArray(original2);
  
   println("Test All elements from Original 2    ");
//  me = new int[] {0, 1, 6, 8};

  me = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  result = matchData(0, 1, me, original2.clone());
  showArray(result);
}

/*
This is the main function
 matchValue     : the value all the elements must match
 newValue       : if all the required elements macth the required value change
                  them to this value
 matchElements  : an array of the elements to be tested
 theArray       : the array we want to test
 
 This function will test all the elements in theArray listed in the matchElements
 array. If they are all valid elements in theArray and they equal the matchValue
 the elements will be changed to the new value.
 */
int[] matchData(int matchValue, int newValue, int[] matchElements, int[] theArray) {
  boolean matched = true; //  assume they are going to match
  for (int i = 0; i <  matchElements.length; i++) {
    int e = matchElements[i];
    matched &= (e >=0 && e < theArray.length && theArray[e] == matchValue);
    if (!matched) break; // element does not exist or does not match required value
  }
  if (matched) {
    for (int i = 0; i <  matchElements.length; i++)
      theArray[matchElements[i]] = newValue;
  }
  return theArray;
}

void showArray(int[] array) {
  for (int i = 0; i < array.length; i++)
    print(array[i] + "   ");
  println();
}

Hi Mister,
Thank you so much for all details.
Your program works very well.
Processing 4 bugs a little, comes it writes bad syntax, but the program runs well.
I put solutions with three cases below.
Thank you very very much :wink:


final int[]
    original = { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  
  cloned = original.clone();
  

final int[]
    original2 = { 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 

  cloned2 = original2.clone();
  
final int[]
    original3 = { 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, 

  cloned3 = original2.clone();

boolean [] bools; 
boolean [] bools2; 
boolean [] bools3; 

void setup() {
  
  println(ifAll0SetTo1(cloned, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); // 
  println(ifAll0SetTo1(cloned, new int[] { 1, 2 })); // false
  
   println(str(original));
   println(str(cloned)); 
   bools = boolean(cloned);
   println(str(bools));

  
    
  println(ifAll0SetTo1(cloned2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); // 
  println(ifAll0SetTo1(cloned2, new int[] { 1, 2 })); // false
   
  println(str(original2));
  println(str(cloned2));
  bools2 = boolean(cloned2);
  println(str(bools2));
  
  println(ifAll0SetTo1(cloned3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); // 
  println(ifAll0SetTo1(cloned3, new int[] { 1, 2 })); // false
   
  println(str(original3));
  println(str(cloned3));
  bools3 = boolean(cloned3);
  println(str(bools3));


 
 

 
 

  exit();
}


@SafeVarargs static final boolean
  ifAll0SetTo1(final byte[] arr, final int... indices)
{
  assert arr.length * indices.length != 0: "Array argument is empty";

  final boolean allZero;
  int sum = 0;

  for (final int idx : indices)  sum += arr[idx];

  if (allZero = sum == 0)  for (final int idx : indices)  arr[idx] = 1;

  return allZero;
}

@SafeVarargs static final boolean
  ifAll0SetTo1(final int[] arr, final int... indices)
{
  assert arr.length * indices.length != 0: "Array argument is empty";

  final boolean allZero;
  int sum = 0;

  for (final int idx : indices)  sum += arr[idx];

  if (allZero = sum == 0)  for (final int idx : indices)  arr[idx] = 1;

  return allZero;
}

That is OK and this makes the problem simpler

We have

  1. a working array where we want to find multiple elements that match a certain value
  2. if there is more than one element matching the required value change them to a different value.

The following sketch demonstrates a method that does this. Note it is much simpler than my previous solution because the problem is a simpler one.

int[] original = { 3, 9, 2, 3, 0, 4, 0, 1, 5, 9, 0, 8 };
int[] result;

void setup() {
  println("Original");
  showArray(original);
  // These tests used a copy of the original array so that we can perform multiple
  // test using the same working array
  println("Convert multiple 0s to 1s (3 values changed)");
  result = multiMatchData(0, 1, original.clone());
  showArray(result);
  println("Convert multiple 2s to 0s (unchanged only one 2)");
  result = multiMatchData(2, 0, original.clone());
  showArray(result);
  println("Convert multiple 7s to 0s (unchanged there are no 7s)");
  result = multiMatchData(7, 10, original.clone());
  showArray(result);
}

/*
This is the main function
 matchValue     : the value we are searching for
 newValue       : if we have more than one element equal to matchValue change it
                  to newValue
 theArray       : the array we want to test
 
 This function will test all the elements in theArray looking for all elements that
 equal the matchValue. If more than one is found then change them all to newValue
 */
int[] multiMatchData(int matchValue, int newValue, int [] theArray) {
  IntList list = new IntList();
  for (int i = 0; i <  theArray.length; i++)
    if (theArray[i] == matchValue) list.append(i);
  if (list.size() > 1)
  list.forEach(element -> { theArray[element] = newValue; } );
  return theArray;
}

void showArray(int[] array) {
  for (int i = 0; i < array.length; i++)
    print(array[i] + "   ");
  println();
}
1 Like

Haven’t installed P4 yet, so I’m still using P3.5.4, b/c P4 still behaves as a buggy beta! :bug:

Anyways, I’ve upgraded the sketch to use 2d arrays plus 1 loop: :loop:

/**
 * Set 1 If All Indices Are 0 (v1.2.2)
 * GoToLoop (2022/Dec/12)
 *
 * https://Discourse.Processing.org/t/logical-test-to-know-if-at-least-
 * one-data-has-the-same-value-as-another/40133/17
 */

final int[][] originals = {
  { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 
  { 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 
  { 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 }
};

final int[][] clones = new int[originals.length][];

final boolean[][] bools = new boolean[originals.length][];

final int[]
  allIndices = IntList.fromRange(originals[0].length).array(), 
  indexPair1And2 = { 1, 2 };

void setup() {
  for (int i = 0; i < originals.length; ++i) {
    clones[i] = originals[i].clone();

    println("\nArray " + i + ':');
    print(ifAll0SetTo1(clones[i], allIndices), TAB);
    println(ifAll0SetTo1(clones[i], indexPair1And2));

    println(str(originals[i]));
    println(str(clones[i]));

    bools[i] = boolean(clones[i]);
    println(str(bools[i]));
  }

  exit();
}

@SafeVarargs static final boolean
  ifAll0SetTo1(final int[] arr, final int... indices)
{
assert arr.length * indices.length != 0: 
  "Passed array argument is empty (length = 0)";

  final boolean allZero;
  int sum = 0;

  for (final int idx : indices)  sum += arr[idx];

  if (allZero = sum == 0)  for (final int idx : indices)  arr[idx] = 1;

  return allZero;
}
Array 0:
false 	false
1 0 1 1 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 1 1 1
true false true true true true true true true true true true

Array 1:
false 	true
1 0 0 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
true true true true true true true true true true true true

Array 2:
false 	false
1 0 1 0 1 1 1 1 1 1 1 1
1 0 1 0 1 1 1 1 1 1 1 1
true false true false true true true true true true true true

Hi.

I’m sorry, I have not made the good request :sweat_smile:

Actually my request is: if at least two datas has the value== 0, so all datas are 1. And if only one data has the value 0 we don’t have to change anything.

I don’t have to compare pair of datas but all datas at the same time. (but this option can be great for further research ;))

I put your former program below. I added real datas I want to compare in the setup in order to put them in the array original3 but i didn’t manage, it is not the good syntax.

int [] TrigmodPos = new int [12];

final int[]
    original = { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1},  
  cloned = original.clone();
  

final int[]
    original2 = { 1, 0, 0, 1, 1, 1, 1, 1, 1, 1}, 

  cloned2 = original2.clone();
  
final int[]
    original3 = { 1, 0, 1, 0, 1, 1, 1, 1, 1, 1}, 

  cloned3 = original3.clone();

boolean [] bools, bools2, bools3; 


void setup() {
  
TrigmodPos[11]=1;
TrigmodPos[10]=0;
TrigmodPos[9]=1;
TrigmodPos[8]=0;
TrigmodPos[7]=1;
TrigmodPos[6]=1;
TrigmodPos[5]=1;
TrigmodPos[4]=1;
TrigmodPos[3]=1;
TrigmodPos[2]=1;


//  original3 =  {TrigmodPos[11], TrigmodPos[10], TrigmodPos[9], TrigmodPos[8], TrigmodPos[7],
//                TrigmodPos[6], TrigmodPos[5], TrigmodPos[4], TrigmodPos[3], TrigmodPos[2]};   

    
  println(ifAll0SetTo1(cloned, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); // 
//  println(ifAll0SetTo1(cloned, new int[] { 1, 2 })); // false
  
   println(str(original));
   println(str(cloned)); 
   bools = boolean(cloned);
   println(str(bools));

  
    
  println(ifAll0SetTo1(cloned2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); // 
//  println(ifAll0SetTo1(cloned2, new int[] { 1, 2 })); // false
   
  println(str(original2));
  println(str(cloned2));
  bools2 = boolean(cloned2);
  println(str(bools2));
  
  println(ifAll0SetTo1(cloned3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); // 
//  println(ifAll0SetTo1(cloned3, new int[] { 1, 2 })); // false
   
  println(str(original3));
  println(str(cloned3));
  bools3 = boolean(cloned3);
  println(str(bools3));


  exit();
}


@SafeVarargs static final boolean
  ifAll0SetTo1(final byte[] arr, final int... indices)
{
  assert arr.length * indices.length != 0: "Array argument is empty";

  final boolean allZero;
  int sum = 0;

  for (final int idx : indices)  sum += arr[idx];

  if (allZero = sum == 0)  for (final int idx : indices)  arr[idx] = 1;

  return allZero;
}

@SafeVarargs static final boolean
  ifAll0SetTo1(final int[] arr, final int... indices)
{
  assert arr.length * indices.length != 0: "Array argument is empty";

  final boolean allZero;
  int sum = 0;

  for (final int idx : indices)  sum += arr[idx];

  if (allZero = sum == 0)  for (final int idx : indices)  arr[idx] = 1;

  return allZero;
}

Hi Quarks!

I have tried your program with my datas and it works. :wink:
Thank you so much!
But now, I would to put my variable inside your array original.
I tried this but it is not the good syntax.

int [] TrigmodPos = new int [12];

int[] original = { 1, 0, 0, 1, 1, 1, 1, 1, 1, 1};
int[] result;

void setup() {
  println("Original");
    
TrigmodPos[11]=1;
TrigmodPos[10]=0;
TrigmodPos[9]=0;
TrigmodPos[8]=1;
TrigmodPos[7]=1;
TrigmodPos[6]=1;
TrigmodPos[5]=1;
TrigmodPos[4]=1;
TrigmodPos[3]=1;
TrigmodPos[2]=1;

// not good syntax!

//original =  {TrigmodPos[11], TrigmodPos[10], TrigmodPos[9], TrigmodPos[8], TrigmodPos[7],
//             TrigmodPos[6], TrigmodPos[5], TrigmodPos[4], TrigmodPos[3], TrigmodPos[2]};   

  
  showArray(original);
  // These tests used a copy of the original array so that we can perform multiple
  // test using the same working array
  println("Convert multiple 0s to 1s (good)");
  result = multiMatchData(0, 1, original.clone());
  showArray(result);
  
  
 /* 
  println("Convert multiple 2s to 0s (unchanged only one 2)");
  result = multiMatchData(2, 0, original.clone());
  showArray(result);
  println("Convert multiple 7s to 0s (unchanged there are no 7s)");
  result = multiMatchData(7, 10, original.clone());
  showArray(result);
*/  
}

/*
This is the main function
 matchValue     : the value we are searching for
 newValue       : if we have more than one element equal to matchValue change it
                  to newValue
 theArray       : the array we want to test
 
 This function will test all the elements in theArray looking for all elements that
 equal the matchValue. If more than one is found then change them all to newValue
 */
int[] multiMatchData(int matchValue, int newValue, int [] theArray) {
  IntList list = new IntList();
  for (int i = 0; i <  theArray.length; i++)
    if (theArray[i] == matchValue) list.append(i);
  if (list.size() > 1)
  list.forEach(element -> { theArray[element] = newValue; } );
  return theArray;
}

void showArray(int[] array) {
  for (int i = 0; i < array.length; i++)
    print(array[i] + "   ");
  println();
}

You don’t put ‘your array in original’ you ‘replace original with your array’ like this

int [] TrigmodPos = new int [12];
int[] result;

void setup() {

  // Init array
  TrigmodPos[11]=1;
  TrigmodPos[10]=0;
  TrigmodPos[9]=0;
  TrigmodPos[8]=1;
  TrigmodPos[7]=1;
  TrigmodPos[6]=1;
  TrigmodPos[5]=1;
  TrigmodPos[4]=1;
  TrigmodPos[3]=1;
  TrigmodPos[2]=1;
  TrigmodPos[1]=0;
  TrigmodPos[0]=0;


  println("Original array");
  showArray(TrigmodPos);
  // These tests used a copy of the original array so that we can perform multiple
  // test using the same working array
  println("Convert multiple 0s to 1s (good)");
  result = multiMatchData(0, 1, TrigmodPos.clone());
  showArray(result);
}

/*
 This is the main function
 matchValue     : the value we are searching for
 newValue       : if we have more than one element equal to matchValue change it
 to newValue
 theArray       : the array we want to test
 
 This function will test all the elements in theArray looking for all elements that
 equal the matchValue. If more than one is found then change them all to newValue
 */
int[] multiMatchData(int matchValue, int newValue, int [] theArray) {
  IntList list = new IntList();
  for (int i = 0; i <  theArray.length; i++)
    if (theArray[i] == matchValue) list.append(i);
  if (list.size() > 1)
  list.forEach(element -> { theArray[element] = newValue; } );
  return theArray;
}

void showArray(int[] array) {
  for (int i = 0; i < array.length; i++)
    print(array[i] + "   ");
  println();
}

The method multiMatchData will work with any integer array :smile:

1 Like