Java Program to Find Smallest and Largest Element in an Array

Table of Contents

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions.

In this post, we will see how to find smallest and largest element in an array.

Problem:

You are given an array of numbers. You need to find smallest and largest numbers in the array.

Solution:

  • Initialise two variable largest and smallest with arr[0]
  • Iterate over array
    • If current element is greater than largest, then assign current element to largest.
    • If current element is smaller than smallest, then assign current element to smallest.
  • You will get smallest and largest element in the end.

Java code to find Smallest and Largest Element in an Array :

When you run above program, you will get below output:

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 02 December

    Pass Variable From PHP to JavaScript

    Table of ContentsUse echo methodUse json_encode methodUse htmlspecialchars method Use echo method In order to pass variable from PHP to JavaScript, you will need to use the echo statement. This will print out the variable for you. For example, if you have a variable named $name, you would use the following code: [crayon-638b2fcbbb084183734991/] This will […]

  • 02 December

    Remove Quotes from String in PHP

    Table of ContentsRemove Quotes from String in PhpUsing str_replace() MethodUsing preg_replace() MethodRemove Quotes from Beginning and End of String in PhpUsing trim() MethodUsing preg_Replace() Method Remove Quotes from String in Php Using str_replace() Method Use the str_replace() function to remove quotes from String in PHP. [crayon-638b2fcbbb6ae346686409/] [crayon-638b2fcbbb6b4977567620/] We used str_replace() to remove double quotes and […]

  • 30 November

    Check if Array is Empty in PowerShell

    Table of ContentsWhat is an Empty Array?Use the Count Property to Check If an Array is Empty in PowerShellUse the Length Property to Check If an Array is Empty in PowerShell 💡TL;DR To check if an array is empty access its count property e.g. arr.count. If count is equal to 0, then array is empty. […]

  • 30 November

    Split Integer into Digits in Python

    Table of ContentsUsing List ComprehensionUsing for LoopUsing map() with list() & str.split() Using List Comprehension To split integer into digits in Python: Use the str() to transform the specified integer to a string. Use a list comprehension to loop over converted String. Use int() to convert every substring to an integer in each iteration. [crayon-638b2fcbbc1cf449482706/] […]

  • 29 November

    Get Length of String in PowerShell

    Table of ContentsUsing String’s Length propertyUsing Measure-Object with Character parameter Using String’s Length property Use string’s length property to get length of String in PowerShell e.g. "Hello World".Length. System.String‘s Length property returns number of Characters present in the String. [crayon-638b2fcbbc726376802836/] Output [crayon-638b2fcbbc72b990241291/] Here, we declared a string variable blog_Name which contains string Java2blog. We have […]

  • 29 November

    Prefix b Before String in Python

    Table of ContentsPrefix b Before String in PythonConclusion Prefix b Before String in Python Prefix b before String denotes a byte String. By putting b before String, you can convert String to bytes in Python. The upgrade from Python 2 to Python 3 was considered a major change as many new features were introduced and […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.