Skip to content

Instantly share code, notes, and snippets.

@Dimanaux
Created November 22, 2019 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dimanaux/d5ce89ea1d5315c2f4e66376668de12a to your computer and use it in GitHub Desktop.
Save Dimanaux/d5ce89ea1d5315c2f4e66376668de12a to your computer and use it in GitHub Desktop.
public class Main {
// declaration: int x;
// initialization: x = 4;
// int x = 4;
public static void main(String[] args) {
int[] array = new int[] {1, 2, 3, 4, 5, 1};
// 1 <= x <= 100; where x from array
// E = exists - if 0 exists in the array
// A = all - if all the array elements are zeroes
/*
boolean containsZero = false;
loop : for (int i = 0; i < array.length; i++) {
if (array[i] == 0) {
containsZero = true;
break loop;
}
}
boolean allZeroes = true;
for (int i = 0; i < array.length; i++) {
if (array[i] != 0) {
allZeroes = false;
break;
}
}
boolean notAllZeroes = true;
int m = 0;
while (m < array.length) {
if (array[m] == 0) {
notAllZeroes = false;
}
m++;
}
int zeroesCount = 0;
int n = 0;
while (n < array.length) {
if (array[n] == 0) {
zeroesCount++;
}
n++;
}
*/
// & &&
// int a = 3;
// int b = -4;
// System.out.println(isGreaterThan5(a) && isLessThan0(b));
// System.out.println();
// System.out.println(isGreaterThan5(a) & isLessThan0(b));
// int[] array = new int[] {1, 2, 3, 4, 5};
boolean twoEqualElements = false;
outer: for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i] == array[j] && i != j) {
twoEqualElements = true;
break outer;
}
}
}
System.out.println(twoEqualElements);
twoEqualElements = false;
for (int i = 0; i < array.length && !twoEqualElements; i++) {
for (int j = 0; j < array.length && !twoEqualElements; j++) {
if (array[i] == array[j] && i != j) {
twoEqualElements = true;
}
}
}
// check if there are 2 ones
// {1, 1, 1, 2, 3, 4, 5...}
int ones = 0;
for (int i = 0; i < array.length && ones < 2; i++) {
if (array[i] == 1) {
ones = ones + 1;
}
// if (ones >= 2) {
// break;
// }
}
System.out.println(ones >= 2);
// count elements in the array
int[] repeats = new int[101];
for (int i = 0; i < array.length; i++) {
repeats[ array[i] ] += 1;
}
System.out.println(repeats[1]);
int max = array[0];
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
int[] repeats1 = new int[max + 1];
for (int i = 0; i < array.length; i++) {
repeats1[ array[i] ] += 1;
}
System.out.println(repeats1[1]);
// swap maximum and minimum in the array
int maxAtIndex = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] > array[maxAtIndex]) {
maxAtIndex = i;
}
}
int minAtIndex = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] < array[minAtIndex]) {
minAtIndex = i;
}
}
int temp = array[maxAtIndex];
array[maxAtIndex] = array[minAtIndex];
array[minAtIndex] = temp;
}
static boolean isGreaterThan5(int x) {
System.out.println("Is greater than 5 ? for " + x);
return x > 5;
}
static boolean isLessThan0(int x) {
System.out.println("Is less than 0 ? for " + x);
return x < 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment