Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: How to find maximum, minimum and occurence time of index in an array list

  1. #1
    Junior Member
    Join Date
    Sep 2022
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to find maximum, minimum and occurence time of index in an array list

    I have been given an exercise to find how many times has a certain index (content value) has been occured as well as the maximum and minimum value index in an array list. For example, if the user inputs the following [10, 10, 20, 20, 30, 40 , 50] and picks the number 20 to see how many times it has occured or listed in an array, it should give an output similar to the following:

    10
    10
    20
    20
    30
    40
    50
     
    Search for? 20
    20 is at index 2
    20 is at index 3

    1. How can I repeat or make the computer count all the value content of a specific number with their index? I do not know where to begin but here is some of my coding and I have been stuck for a while in this one:

    2. After counting a specific value content of an index, how can I identify the maximum and minimum value content of an index without using collections.max or min nor any other methods. In other words, I am trying to find a linear or mathematical solution to the problem please.

    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.Collections;
     
    public class IndexOf {
     
        public static void main(String[] args) 
        {
            Scanner scanner = new Scanner(System.in);
            ArrayList<Integer> arrayList = new ArrayList<>();
     
            while (true) 
            {
                int input = scanner.nextInt();
                if (input == -1) 
                {
                    break;
                }
                arrayList.add(input);
            }
            System.out.println("search for? ");
            int input2 = scanner.nextInt();
            System.out.println(input2 + " is at index " + arrayList.indexOf(input2));
     
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    If all the numbers have been loaded into a list, can you write code to go through the list and print out its contents?
    Then instead of printing the contents, change the code to compare each element in the list against the value to search for.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2022
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    Quote Originally Posted by Norm View Post
    If all the numbers have been loaded into a list, can you write code to go through the list and print out its contents?



    Then instead of printing the contents, change the code to compare each element in the list against the value to search for.
    Yes the code would be: system.out.println(arrayList); after the while loop to list [all the array elements I have inputted], do correct me if I am wrong please.

    If I have 5 elements in an array list, why do I have to compare 5 elements against the value that I am searching for? what is the idea behind it?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    system.out.println(arrayList);
    That would show all the elements in one line.
    I was asking for code that would show each element on its own line. That was preparing you to write code to compare the elements instead of printing them.

    why do I have to compare 5 elements against the value that I am searching for? what is the idea behind it?
    If you have a list and what to find one or more elements in the list, you need to access each one and compare its contents against the desired value.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2022
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    Quote Originally Posted by Norm View Post
    That would show all the elements in one line.
    I was asking for code that would show each element on its own line. That was preparing you to write code to compare the elements instead of printing them.


    If you have a list and what to find one or more elements in the list, you need to access each one and compare its contents against the desired value.
    public class IndexOf {
     
        public static void main(String[] args) 
        {
            Scanner scanner = new Scanner(System.in);
            ArrayList<Integer> arrayList = new ArrayList<>();
            int input;
            while (true) 
            {
                input = scanner.nextInt();
                if (input == -1) 
                {
                    break;
                }
                arrayList.add(input);
            }
            System.out.println(arrayList.get(2));
            System.out.println(arrayList.get(3));
     
        }
    }

    This how to access each and every element, but this is not a practical approach because if I have an array list of 10 to 20 elements, I have to print out many code lines for printing.

    As for comparing, I still do not understand what I am comparing elements to the value that I am searching for, do you mean that I am comparing elements to find numbers/elements that are in common with the desired value?

  6. #6
    Junior Member
    Join Date
    Sep 2022
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    for (int loopValue = 0; loopValue < arrayList.size(); loopValue++) {
                System.out.println(arrayList.get(loopValue));
            }

    I believe this is what you were referring to, printing out each and every element individually in each line?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    printing out each and every element individually
    Yes, that loop looks at each element in the list one at a time, starting with the first and going to the last.

    Now change the print statement to be controlled by a if statement that will only be true when element from the list is equal to a desired value that is given by the user. Use the equals method to do the compare.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Sep 2022
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    Quote Originally Posted by Norm View Post
    Yes, that loop looks at each element in the list one at a time, starting with the first and going to the last.

    Now change the print statement to be controlled by a if statement that will only be true when element from the list is equal to a desired value that is given by the user. Use the equals method to do the compare.

    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class IndexOf {
     
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            ArrayList<Integer> arrayList = new ArrayList<>();
            int input;
            while (true) {
                input = scanner.nextInt();
                if (input == -1) {
                    break;
                }
                arrayList.add(input);
            }
            System.out.println("search for? ");
     
            for (int loopValue = 0; loopValue < arrayList.size(); loopValue++) {
                int input2 = scanner.nextInt();
                if (input == input2) {
                    System.out.println(arrayList.get(loopValue));
     
                }
            }
     
        }
    }

    After searching for the value to input, it keeps printing the 2nd input value that I have entered 5 times according to the loop and its condition. I am still confused on how to approach this method and is there a possibility not to use any method such as hashmap, collections or the equals method? I am trying to find a mathematical or linear approach without using methods that automate or instantly give the answer or what I am looking for

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    The code to read the user's input is inside of the loop. That means the user is required to enter a value every time the loop goes around. Is that what you want? I thought the user's input would be obtained one time before the search loop and then the loop would be used to compare each element in the list against the user's input.
    Since the user's input is an int and the list contains Integer objects, there needs to be code to get the int value from the Integer object before using the == to compare their values.

    if (input == input2) {
    The code is NOT comparing the element from the list against the user's input. I would expect input to have the -1 value that was detected to exit the first loop.
    Use the get method to access an element in the list (See how the print statement does it) and then compare that value with what the user entered.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Sep 2022
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    Quote Originally Posted by Norm View Post
    The code to read the user's input is inside of the loop. That means the user is required to enter a value every time the loop goes around. Is that what you want? I thought the user's input would be obtained one time before the search loop and then the loop would be used to compare each element in the list against the user's input.
    Since the user's input is an int and the list contains Integer objects, there needs to be code to get the int value from the Integer object before using the == to compare their values.


    The code is NOT comparing the element from the list against the user's input. I would expect input to have the -1 value that was detected to exit the first loop.
    Use the get method to access an element in the list (See how the print statement does it) and then compare that value with what the user entered.
    1. My bad, I only want the user to input a number that is within the array list once, I will put that outside the loop and before it.
    2. from my understanding to the 'there needs to be a code to get the integer value from the integer elements(objects)' do you mean i need to assign a new integer variable to the array list and compare it with the new input which is input2? Because if I say input == input2 that includes the -1 indeed and it is out of the index and cannot be compared to the searchingValue input.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    get the integer value from the integer elements(objects)
    Look at the API doc for the Integer class to see what method to call to get its int value. There is no need for a new variable.
    Your code would look something like:
    if(usersInput == list.get(index).getIntValue()) { ...
    It is possible to chain calls to methods together when the preceding method (get) returns an object value (an Integer) then one of that object's methods (getIntValue) can be called on that object.
    Look at the API doc for Integer to get the correct method name.

    list.get(index).getIntValue()
    Breaking that into separate steps would be:
    Integer anInt = list.get(index);    // get object from list
    int value = anInt.getIntValue();   // get int from Integer
    now value could be compared to the user's input. Normally people chain the methods.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Sep 2022
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    Quote Originally Posted by Norm View Post
    Look at the API doc for the Integer class to see what method to call to get its int value. There is no need for a new variable.
    Your code would look something like:

    It is possible to chain calls to methods together when the preceding method (get) returns an object value (an Integer) then one of that object's methods (getIntValue) can be called on that object.
    Look at the API doc for Integer to get the correct method name.
    Now at this part I am kind of lost, please give me time to look for it and try to understand what you are saying.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    The java SE API doc: https://docs.oracle.com/javase/8/docs/api/index.html
    Find the class you are interested in the lower left frame, click it and the API doc for that class will be displayed in the main window.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Jun 2022
    Posts
    41
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default Re: How to find maximum, minimum and occurence time of index in an array list

    I wrote a solution but was advised against posting complete solutions, as it tends to attract those who want only to pass a test without exercising skill. There's some loss of learning by example, but it also reduces placing the incompetent in jobs where those skills are required, so it's a chosen policy I shall abide.

    The requirement is to produce an output something like:
    Quote Originally Posted by output
    3 occurrences of the value 10 in the index range (1 ... 7), at indexes [1, 2, 7]
    To get maximum (and minimum) indexes, we can first set a variable maxIndex to it's smallest value. Then while looping through each test case, compare a found index against maxIndex to update it.
    To get the list of indexes, add each found index to a list of foundIndexes

    Yes I know it's indices, but that's just so odd XD
    Last edited by AngleWyrm; September 15th, 2022 at 09:47 PM.

Similar Threads

  1. I need help on creating a method to handle minimum and maximum input values
    By Manzanita in forum Object Oriented Programming
    Replies: 1
    Last Post: April 2nd, 2014, 02:22 AM
  2. reading floats from file and output minimum and maximum temperatures
    By james_b in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 3rd, 2013, 11:10 PM
  3. Replies: 3
    Last Post: August 26th, 2013, 04:41 PM
  4. Find maximum working time [Urgent]
    By loong424 in forum Algorithms & Recursion
    Replies: 4
    Last Post: August 22nd, 2013, 03:18 AM
  5. Java program to find the minimum and maximum values of input data
    By awake77 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 20th, 2008, 05:12 PM