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

**URL:** <https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133>\
**Category:** Coding Questions\
**Created:** [December 12, 2022, 1:55pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133 "2022-12-12T13:55:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [December 12, 2022, 1:55pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/1 "2022-12-12T13:55:04Z")

</div>

Dear you 😉

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 😘

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 12, 2022, 1:57pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/2 "2022-12-12T13:57:45Z")

</div>

> [@bking](#):
>
> I’m trying to find a way to compare two datas or more, in order to know if they has the same value.

Same values or if they’re all `0` only?

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [December 12, 2022, 2:18pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/3 "2022-12-12T14:18:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 12, 2022, 2:29pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/4 "2022-12-12T14:29:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [December 12, 2022, 2:43pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/5 "2022-12-12T14:43:58Z")

</div>

Yes exactly what i would like 👌

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 12, 2022, 3:17pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/6 "2022-12-12T15:17:44Z")

</div>

- 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[]`:

```auto
/**
 * 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;
}

```

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [December 12, 2022, 4:53pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/7 "2022-12-12T16:53:53Z")

</div>

I am not familiarized at all with a lot of term in your program. 😶‍🌫️  
Here what I did

```auto
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();
 } 

```

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [December 15, 2022, 8:11pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/8 "2022-12-15T20:11:04Z")

</div>

> [@GoToLoop](#):
>
> ```auto
> 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;
> }
> 
> ```

Could you detail, or showing example please?

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [December 15, 2022, 8:15pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/9 "2022-12-15T20:15:30Z")

</div>

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!

```auto

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();
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 16, 2022, 12:31am UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/10 "2022-12-16T00:31:59Z")

</div>

> [@bking](#):
>
> Could you detail, or showing example please?

- 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()**](https://processing.org/reference/booleanconvert_.html).
- Here’s another **setup()** example.
- Just re-use function **ifAll0SetTo1()** from my original sketch:

```auto
/**
 * 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();
}

```

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [December 16, 2022, 6:57pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/11 "2022-12-16T18:57:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 16, 2022, 7:55pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/12 "2022-12-16T19:55:39Z")

</div>

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 😄

```auto
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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 17, 2022, 6:06am UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/13 "2022-12-17T06:06:59Z")

</div>

> [@bking](#):
>
> Could you make at least half of the program work so I can understand better please?

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.

> [@bking](#):
>
> , but I haven’t learned Processing with terms like _ **arguments** _…

> **[Argument - MDN Web Docs Glossary: Definitions of Web-related terms | MDN](https://developer.mozilla.org/en-US/docs/Glossary/Argument)**
>
> An argument is a value (primitive or object) passed as input to a function.

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

> **[Parameter - MDN Web Docs Glossary: Definitions of Web-related terms | MDN](https://developer.mozilla.org/en-US/docs/Glossary/Parameter)**
>
> A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions.

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](https://en.wiktionary.org/wiki/variadic#Adjective) (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:

> [@bking](#):
>
> I’m really trying to understand what you said,

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

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [December 17, 2022, 10:52pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/14 "2022-12-17T22:52:00Z")

</div>

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

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [December 17, 2022, 11:06pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/15 "2022-12-17T23:06:21Z")

</div>

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 😉

```auto

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;
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 18, 2022, 9:39am UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/16 "2022-12-18T09:39:26Z")

</div>

> [@bking](#):
>
> I think I asked my request in a bad manner. I 'm totally sorry.

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.

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 18, 2022, 10:29am UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/17 "2022-12-18T10:29:14Z")

</div>

> [@bking](#):
>
> Processing 4 bugs a little, comes it writes bad syntax, but the program runs well.

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

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

```auto
/**
 * 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

```

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [December 18, 2022, 12:15pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/18 "2022-12-18T12:15:57Z")

</div>

Hi.

I’m sorry, I have not made the good request 😅

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.

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [December 18, 2022, 12:28pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/19 "2022-12-18T12:28:50Z")

</div>

Hi Quarks!

I have tried your program with my datas and it works. 😉  
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.

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 18, 2022, 1:56pm UTC](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133/20 "2022-12-18T13:56:35Z")

</div>

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

```auto
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 😄

[Next page](https://discourse.processing.org/t/compare-datas-to-know-if-at-least-one-data-has-the-same-value-as-another/40133.md?page=2)
