@quark has already posted his solution, but it requires P4.
Here’s my version based on my previous solutions but compatible w/ both P3 & P4:
/**
* Set All to New Val If Match Val Found More Than 1 (v1.0.1)
* GoToLoop (2022/Dec/18)
*
* https://Discourse.Processing.org/t/logical-test-to-know-if-at-least-
* one-data-has-the-same-value-as-another/40133/21
*/
static int MATCH_VAL = 0, NEW_VAL = 1, MATCHES = 2;
final int[][] originals = {
{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1 } // TrigmodPos[]
};
final int[][] clones = new int[originals.length][];
final boolean[][] bools = new boolean[originals.length][];
void setup() {
for (int i = 0; i < originals.length; ++i) {
clones[i] = originals[i].clone();
println("\nArray " + i + ':');
println(if2MatchValFoundSetAllToNewVal(MATCH_VAL, NEW_VAL, clones[i]));
println(str(originals[i]));
println(str(clones[i]));
bools[i] = boolean(clones[i]);
println(str(bools[i]));
}
exit();
}
@SafeVarargs static final boolean if2MatchValFoundSetAllToNewVal
(final int matchVal, final int newVal, final int... arr)
{
assert arr.length > 0:
"Passed array argument is empty (length = 0)";
final boolean FoundTwoOrMore;
int count = 0;
for (final int val : arr) if (val == matchVal && ++count >= MATCHES) break;
if (FoundTwoOrMore = count >= MATCHES) java.util.Arrays.fill(arr, newVal);
return FoundTwoOrMore;
}
I didn’t realise we were looking for a pre P4 solution but the change to my code is very simple and the sketch below works with both P3 and P4
EDIT: I missed a line of code when I first posted this replay. I have corrected the code below to include it.
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) // ###### EDIT this line has been added ##########
for (int i = 0; i < list.size(); i++)
theArray[list.get(i)] = newValue;
return theArray;
}
void showArray(int[] array) {
for (int i = 0; i < array.length; i++)
print(array[i] + " ");
println();
}