Skip to main content

Search Integer in Array Java

Question: 

 



package aRray;

import java.util.*;

public class myArray {

public static int checkN(int [] arr, int size, int found ) {

//int [] arr, int size, int found - --- -- these are input types

for(int i = 0; i < size; i++) {

if(found == arr[i]) {

return i;

}

}

return -1;

}

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);

int size = scan.nextInt();

//this is for input

int arr [] = new int[size];

//for loop to store data in ith position

for(int i = 0 ; i < size; i++) {

// 0 is starting index of array so i m starting from 0

arr[i] = scan.nextInt();

//this is for store value in index from user

}

//this loop for output

int found = scan.nextInt(); // this is for found element in array

int result = checkN(arr, size, found);

System.out.println(result);

}


}


Comments