How to Check If Given Int Exists in Array

Java, Simplified check if int array contains int

Here is Java 8 solution

public static boolean contains(final int[] arr, final int key) {
return Arrays.stream(arr).anyMatch(i -> i == key);
}

Check if an element is present in an array

ECMAScript 2016 incorporates an includes() method for arrays that specifically solves the problem, and so is now the preferred method.

[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4); // false
[1, 2, 3].includes(1, 2); // false (second parameter is the index position in this array at which to begin searching)

As of JULY 2018, this has been implemented in almost all major browsers, if you need to support an older browser a polyfill is available.

Edit: Note that this returns false if the item in the array is an object. This is because similar objects are two different objects in JavaScript.

How can I check if given int exists in array?

You can use std::find for this:

#include <algorithm> // for std::find
#include <iterator> // for std::begin, std::end

int main ()
{
int a[] = {3, 6, 8, 33};
int x = 8;
bool exists = std::find(std::begin(a), std::end(a), x) != std::end(a);
}

std::find returns an iterator to the first occurrence of x, or an iterator to one-past the end of the range if x is not found.

Check an Array if it contains an integer in Java

You can use IntStream in Java 8

if (IntStream.of(field).anyMatch(i -> i == 1) &&
IntStream.of(field).anyMatch(i -> i == 2)) {
// has a 1 and a 2

How do I determine whether an array contains a particular value in Java?

Arrays.asList(yourArray).contains(yourValue)

Warning: this doesn't work for arrays of primitives (see the comments).


Since java-8 you can now use Streams.

String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.stream(values).anyMatch("s"::equals);

To check whether an array of int, double or long contains a value use IntStream, DoubleStream or LongStream respectively.

Example

int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);

Check an integer variable against integers already stored in an array

You are doing too much to just fill an array. To fill an array, try using a "for" loop like so:

public class ItemSet {
public static void main(String []args) {
int[] itemSet;
itemSet = new int[5];

int count = 1;
int assignItem = 0;
int countTwo = 1;

for (int i = 0; i < itemSet.length; i++) {
itemSet[i] = (int) (Math.random() * 76);
}
}
}

To print the values stored in the array, try using an enhanced-for loop:

for (int element : itemSet) {
System.out.println(element);
}

To check the values BEFORE storing the next integer, (say to see ensure that the new stored value would be unique) you could use a nested for loop that starts at the value beneath the outer loop and walks backwards, comparing each value that came before to the value that was just stored, and if they are the same, it will decrement the outer counter which will then override the data on the next loop:

import java.util.Arrays; // you need this to use Arrays.toString()

public class ItemSet {
public static void main(String []args) {
int[] itemSet;
itemSet = new int[5];

int count = 1;
int assignItem = 0;
int countTwo = 1;

for (int i = 0; i < itemSet.length; i++) {
itemSet[i] = (int) (Math.random() * 76);
for (int j = i - 1; j >= 0; j--) {
if (itemSet[j] == itemSet[i]) {
System.out.println("Comparing " + itemSet[j] +
" and " + itemSet[i]);
i--;
break; // not really needed but illustrates a way to exit a loop immediately
}
}
}

// this is a handy way to print all the data in an array quickly
System.out.println(Arrays.toString(itemSet));
}
}

Check if an Array contains an Integer or not using for loop

not using an array and a for loop will solve all your issues. try

if (guess >= 1 && guess <= 100) {
System.out.println("good job");
} else {
System.out.println("terrible");
}

1) let's assume guess == 10. Your for loop will print "good job" 99 times. Because 10 != 1, 10 != 2, 10 == 10, 10 != 100.

2) because guess is != i 99 times again.

3) use a while ( true ) loop and a break after printing "good job".



Related Topics



Leave a reply



Submit