0

I have added few number into the arraylist . I would want to find the certain value from it.Example i have 4,4,9,9,18. I would like to find the value of 26. If 26 > largest value in the list it will display 18 and if value is 17 it will display 9, and if value is 5 it will display 4. Also is there another method to implement this search because liner search might be slow.

search value 26

    [4,4,9,9,18] display 18
    [20,20,29,29,4] display 20
    [28,28,28,1,10] display 28

if you have this list and search 26, it will output the first element. because the first element is <= than the value being search.

but current output is

Value of value2 : 9

    public class Arraylist {

    public static ArrayList<Integer> aList;

    public static void main(String[] args) {
        aList = new ArrayList<Integer>();
        aList.add(4);
        aList.add(4);
        aList.add(9);
        aList.add(9);
        aList.add(18);
        int value = 26;
        int value2 = 0;

        for (int i = 0; i < aList.size(); i++) {
            if (aList.get(i) <= value) {          
                if (i + 1 < aList.size()) {
                    value2 = aList.get(i);
                } else if(i > aList.size()) {
                    value2 = aList.get(i);

                }
            }
        }
        System.out.println("Value of value2 : " + value2);
    }
}
18
  • I do not understand your logic. Are you looking for a place where a given value should be inserted into a sorted ArrayList to keep it sorted? Commented Oct 25, 2013 at 4:05
  • Another method could be to start search from the middle of the list, and check if the variable is higher or lower than value in the array, if lower then search backwards from this point and if higher search forwards. Commented Oct 25, 2013 at 4:05
  • So you've been asked to implement something that sounds almost like binary search... Commented Oct 25, 2013 at 4:08
  • If you array is always sorted then binary search will be faster. Commented Oct 25, 2013 at 4:08
  • What if the value you are looking for is 3? Commented Oct 25, 2013 at 4:08

6 Answers 6

1

I have written the code using an array. You can easily adopt it to ArrayList

int a[] = {28,28,28,1,10};
// int a[] = {20,20,29,29,4}; // other input of yours
// int a[] = {4,4,9,9,18};  

   int x = 26;

   int liVal = -1;
   for(int i=0; i<a.length;i++)
       if(x < a[i]) // if we met a value > x
       {
          if(liVal==-1) // if we could not find any largest value smaller than x
              liVal = a[i]; // return the value > x
          break;
       }
       else if(x > a[i]) // find the largest value smaller than x, 
       {
           if(liVal < a[i])
               liVal = a[i];
       }

System.out.println(liVal);
Sign up to request clarification or add additional context in comments.

Comments

0

A trivial and un-optimized version:

int value = 26 // or whatever parameter you get
int retVal = Integer.MIN_VALUE;
for (int i : list) {
  if (i <= value && i > retVal) {
    retVal = i;
  }
}
return retVal;

1 Comment

This won't handle the case where the value < min(list) because the if statement is never entered since i <= value is never true. In that case you will return Integer.MIN_VALUE instead of min(list).
0

If the array is sorted you can use a variant of binary search.
see http://en.wikipedia.org/wiki/Binary_search_algorithm

Comments

0

Once you sort the list, binarySearch in Collections will do the trick:

Collections.sort(aList)
int index = Collections.binarySearch(aList)

If index is nonnegative, the number was found in the list, and index is the position. If it is negative, it wasn't found, but index indicates where it would be if it were in the list.

And with O(log n) runtime for the search.

2 Comments

If you're only going to search a list once, the actual runtime is O(n log n) since you have to sort first.
I was just referring to the search since that is what the OP seemed concerned about. Edited for clarity.
0

If I understand correctly, you want to find the biggest number in your array that is smaller or equal to value. I would do it like this:

for (int i = 0; i < aList.size(); i++) {
    if ( aList.get(i) <= value && aList.get(i) > value2) {
        value2 = aList.get(i);
    }
}

Also in your example, you do value2 = 0. This is ok if you can guarante that the array only contains positive values. Otherwise it would be better to use value2 = Integer.MIN_VALUE.

Finally, this code assume that the array is not guarantied to be sorted and that you will only need to search it once. Otherwise, a binary search could be more performant. Other answers on this question already show how to accomplish that.

5 Comments

@user2822351 : If your ArrayList is unsorted, then straight forward search could be your only option.
How is value2 initialized? What will happen if value is bigger than all elements of the list?
the search suppose to search from [0]-[4] meaning from first number to the last, if value is bigger than all element, it will be the largest being displayed, if the value is =< than first element it will be the largest element in the list example [28,28,28,1,10] and you search 26
Edited for clarity. Indeed here I assumed that the list was not necessarily sorted. I also did not originally commented on the initial value of the value2 variable since the OP was concerned with the search.
I am more concerned of getting the correct display value and than the efficiency of the search
0

According to the OP's comments:

  1. The list isn't sorted
  2. if value < min return min
  3. if value > max return max
  4. if min <= value <= max return nearest val <= value

    public static int findValue(List<Integer> list, int value){
        int min = Integer.MAX_VALUE, nearest = Integer.MIN_VALUE;
        for(Integer v : list){
            if(v == value)
                return value;
            if(v > nearest && v < value)
                nearest = v;
            if(v < min)
                min = v;
        }
        return value < min ? min : nearest;
    }
    

As a side note, you don't need to keep track of the max because nearest = max if value > max(list).

2 Comments

I have added 3 example of list
In your examples the exact rules you are trying to convey are ambiguous. I think you meant the last example to display the number immediately < 26 which is 10. In that case this code will give you the correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.