arrays with for loops

Solutions on MaxInterview for arrays with for loops by the best coders in the world

showing results for - "arrays with for loops"
Hamza
02 Aug 2016
1// Find-Max variant -- rather than finding the max value, find the
2// *index* of the max value
3public int findMaxIndex(int[] nums) {
4  int maxIndex = 0;  // say the 0th element is the biggest
5  int maxValue = nums[0];
6
7  // Look at every element, starting at 1
8  for (int i=1; i<nums.length; i++) {
9    if (nums[i] > maxValue) {
10      maxIndex = i;
11      maxValue = nums[maxIndex];
12    }
13  }
14  return maxIndex;
15}
16
Rick
04 Apr 2019
1int findSmallest(int[] values) {
2  int minIndex = 0;  // start with 0th element as min
3  for (int i=1; i<values.length; i++) {
4    if (values[i] < values[minIndex]) {
5      minIndex = i;
6    }
7  }
8  return minIndex;
9}
10
Frida
12 Nov 2016
1// For-All variant
2// Go through the elements backwards
3// by adjusting the for-loop
4public void forAllBackwards(int[] nums) {
5  for (int i = nums.length-1; i>=0; i--) {
6    System.out.println( nums[i] );
7  }
8}
9
Luciana
15 Feb 2019
1// Find-Max variant
2// Use very small number, Integer.MIN_VALUE,
3// as the initial max value.
4public int findMax2(int[] nums) {
5  int maxSoFar = Integer.MIN_VALUE;  // about -2 billion
6
7  // Look at every element
8  for (int i=0; i<nums.length; i++) {
9    if (nums[i] > maxSoFar) {
10      maxSoFar = nums[i];
11    }
12  }
13  
14  return maxSoFar;
15}
16
Mahamadou
01 Jan 2021
1String[] words = new String[] { "hello", "foo", "bar" };
2int[] squares = new int[] { 1, 4, 9, 16 };
3
Maelyss
07 Jan 2020
1// Given an array of booleans representing a series
2// of coin tosses (true=heads, false=tails),
3// returns true if the array contains anywhere within it
4// a string of 10 heads in a row.
5// (example of a search loop)
6public boolean searchHeads(boolean[] heads) {
7  int streak = 0;     // count the streak of heads in a row
8  
9  for (int i=0; i<heads.length; i++) {
10    if (heads[i]) {   // heads : increment streak
11      streak++;
12      if (streak == 10) {
13        return true;  // found it!
14      }
15    }
16    else {            // tails : streak is broken
17      streak = 0;
18    }
19  }
20  
21  // If we get here, there was no streak of 10
22  return false;
23}
24
Emy
07 May 2019
1// Search
2// Searches through the given array looking
3// for the given target. Returns the index number
4// of the target if found, or -1 otherwise.
5// (classic search-loop example)
6public int search(int[] nums, int target) {
7  // Look at every element
8  for (int i=0; i<nums.length; i++) {
9    if (nums[i] == target) {
10      return i; // return the index where the target is found
11    }
12  }
13
14  // If we get here, the target was not in the array
15  return -1;
16}
17
Jessica
20 Mar 2017
1// For-All
2// Do something for every element
3public void forAll(int[] nums) {
4  for (int i=0; i<nums.length; i++) {
5    System.out.println( nums[i] );
6  }
7}
8
Giorgio
22 May 2016
1// Find-Max
2// Given a non-empty array of ints, returns
3// the largest int value found in the array.
4// (does not work with empty arrays)
5public int findMax(int[] nums) {
6  int maxSoFar = nums[0];  // use nums[0] as the max to start
7
8  // Look at every element, starting at 1
9  for (int i=1; i<nums.length; i++) {
10    if (nums[i] > maxSoFar) {
11      maxSoFar = nums[i];
12    }
13  }
14  return maxSoFar;
15}
16
Lukas
15 Feb 2016
1// Another way to write search that combines
2// the "end of array" and "found" logic in one
3// while loop. As a matter of style, we prefer
4// the above version that uses the standard
5// for-all loop.
6public int searchNotAsGood(int[] nums, int target) {
7  int i = 0;
8  while (i<nums.length && nums[i]!=target) {
9    i++;
10  }
11  // get here either because we found it, or hit end of array
12  if (i==nums.length) {
13    return -1;
14  }
15  else {
16    return i;
17  }
18}
19
queries leading to this page
looping a new array javawhy do we use for loops with arrays 3fhow for loop works in arraysloops and arraysusing for loop java arraysfor loops with arrayshow to create an array using a for loopworking with arrays in a for loophow to do a for loop in arrayarray number loopadding arrays using for loop in javacan you use for of loops on fucntions in arraysarray numbers for loophow to use array in for loopaccessing array in loopwhy should we use arrays with for loopsjava array loopfor loopshow to create arrays in loopcan a for loop work with an arrayloop array of numbershow to loop arrayseach value in the array can be accessed in a for loop using listarrays using for looparray with for loopfor all my arrays codeusing arrays in loopsloop array while in another array 27s loopjava questions arrays and loopscreate an array for for looparrays with for loopsgetting array with for loopjava array loopingnested for loopsfor loop array program 1 to 10 how number in an array using for loopdeclare new array in a for loopcan i use a 27for loop to create an array in javafor loops using arrayscreate an array using for loopfor loop and arraysworking with arrays for loopscollection min vs iterate through array timejava examples 2b arrays 2b loopsaccessing elements of an array in java using a for loophow to write an array with a for loopmethods for looping the arrayjava loops process data in arrayfor loops arraysdeclare array in for loopfor loops in arrayshow to create a for loop for an arraycreate an array via for loopfor loop arrayshow to use arrays in for loopsjava array with looploop array in ooparray and loop javafor loop create arrayhow do you use loops in arrays 3floop till all index found java arrayjava 2b programs 2b arrays 2b loopswhich loop would you use to easily output only the array values without having to declare index and increment 3flooping arrays numberslooping a new arrayhow to do a for loop for arraysjava how to decrease all the array elements in each iterationfor loop java array tutorialuseing arrays in for loopsread array with loopjava write to an array using for loophow to create arrays using loop 3fhow to create an loop array in javafor loops and arraysloops for arrayshow to create an an array in for looploop type for arraycan we declare and use an array in loophaving an array in a for loophow to create arrays in for looparrays with for loops