How to find the lowest number in an array?

butter_baby

I am writing an algorithm to find the lowest number in an array however my print statement keeps saying that the lowest number is 0. I have the following:

var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]

func findMin(numbers: NSArray) {

    var minValue = numbers[0]
    var isSmallest: Bool

    for i in 0...numbers.count {
        isSmallest = true
        for j in 0...numbers.count {
            if i > j {
                isSmallest = false
            }
        }

        if isSmallest {
            minValue = i
        }

    }
    print("Smallest value in the list is \(minValue)")
}

findMin(numbers: list as NSArray)

My print statement returns as:

"Smallest value in the list is 0\n"

I feel like the algorithm is correct. Any ideas?

EDIT: Answered my own question

I was iterating over indices and not actual values. Thanks to one of the users in the comments. The correct code should be:

var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]

func findMin(numbers: NSArray) {

    var minValue = numbers[0]
    var isSmallest: Bool

    for i in list {
        isSmallest = true
        for j in list {
            if i > j {
                isSmallest = false
            }
         }

        if isSmallest {
            minValue = i
        }

    }

    print("Smallest value in the list is \(minValue)")
}

findMin(numbers: list as NSArray)
vadian

Simply

let list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
let minValue = list.min()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to find the lowest number an object

Find the lowest unused number in an array using Javascript

find the highest and lowest number in array with JOptionPane

How to find the highest and lowest number with JS?

How to find the lowest number of a grouped dataframe in Python

How to find the "lowest" element from array<struct>?

How to find lowest number using criteria from 2 columns

How do I find the max in an Array using only math algorithms, specifically I am trying to knock out the lowest number each time

SQL - Find the lowest unused number

Find the lowest number without comparing

Ruby on rails how to take lowest/biggest number from array

How to round variable values down to next lowest number from an array?

How to find the lowest possible combination of keys within an array

How to find the lowest value in an array generated from the random class?

How to I find lowest and highest value of this array (java)

How can I find highest and lowest in an array of objects/ Javascript

Finding lowest and highest number number value in an Array

How can I find the lowest number in a range, and increase that number by 1 in google sheets?

How to get the highest and lowest number

How to find the lowest value in a hashmap?

How to find the lowest value in a HashMap

All permutations, and find the lowest number c++

Find highest and lowest number from the string of numbers

MySQL: Find lowest free number or append

Can't find the lowest number value in this dict

Find the lowest PHP array value (specific) and return lowest key

How to find the number of elements in an array?

How to find a largest number in an array?

how can I find the lowest value of each row and exclude them to find a new average from array