Finding the second smallest integer in array

后端 未结 19 1882
無奈伤痛
無奈伤痛 2021-01-18 05:48

We are required in our assignment to find the second smallest integer in one array recursively. However, for the sake of understanding the subject more, I want to do it iter

相关标签:
19条回答
  • 2021-01-18 06:33

    Try this one. Second condition is used to catch an event when the smallest number is the first

        int[] elements = {-5, -4, 0, 2, 10, 3, -3};
        int smallest = Integer.MAX_VALUE;
        int secondSmallest = Integer.MAX_VALUE;
        for (int i = 0; i < elements.length; i++) {
            if(elements[i]==smallest){
              secondSmallest=smallest;
            } else if (elements[i] < smallest) {
                secondSmallest = smallest;
                smallest = elements[i];
            } else if (elements[i] < secondSmallest) {
                secondSmallest = elements[i];
            }
    
        }
    

    UPD by @Axel

    int[] elements = {-5, -4, 0, 2, 10, 3, -3};
    int smallest = Integer.MAX_VALUE;
    int secondSmallest = Integer.MAX_VALUE;
    for (int i = 0; i < elements.length; i++) {
        if (elements[i] < smallest) {
            secondSmallest = smallest;
            smallest = elements[i];
        } else if (elements[i] < secondSmallest) {
            secondSmallest = elements[i];
        }
    }
    
    0 讨论(0)
  • 2021-01-18 06:35

    How about this?

       int[] result = Arrays.asList(-3, 4,-1,-2).stream()
                .reduce(new int[]{Integer.MIN_VALUE, Integer.MIN_VALUE},
                        (maxValues, x) -> {
                            if (x > maxValues[0]) {
                                maxValues[1] = maxValues[0]; //max becomes second max
                                maxValues[0] = x;
                            }
                            else if (x > maxValues[1]) maxValues[1] = x;
                            return maxValues;
                        }
                        , (x, y) -> x);
    
    0 讨论(0)
  • 2021-01-18 06:35
         public static void main(String[] args) 
    {
        int[] elements  = {-4 , 2 , 10 , -2, -3 }; 
        int smallest = Integer.MAX_VALUE; 
        int secondSmallest = Integer.MAX_VALUE; 
    
        for (int i = 0; i < elements.length; i++)
        {
            if (smallest>elements[i])
                smallest=elements[i];
    
        }
    
        for (int i = 0; i < elements.length; i++)
        {
            if (secondSmallest>elements[i] && elements[i]>smallest)
                secondSmallest=elements[i];
    
        }
    
        System.out.println("The smallest element is: " + smallest + "\n"+  "The second smallest element is: " + secondSmallest);
    }
    
    0 讨论(0)
  • 2021-01-18 06:36

    Try this ... First condition checks if both values are less than value in array. Second condition if value is less than small than smallest=element[i] else secondSmallest=elements[i]..

    public static void main(String[] args) 
    {
        int[] elements  = {0 , 2 , 10 , 3, -3 }; 
        int smallest = elements[0];
        int secondSmallest = 0; 
    
          for (int i = 0; i < elements.Length; i++)
            {
                if (elements[i]<smallest || elements[i]<secondSmallest )
                {
                    if (elements[i] < smallest )
                    {
                        secondSmallest  = smallest ;
                        smallest = elements[i];
    
                    }
                    else
                    {
                        secondSmallest = elements[i];
    
                    }
                }
            }
    
        System.out.println("The smallest element is: " + smallest + "\n"+  "The second smallest element is: " + secondSmallest);
    }
    
    0 讨论(0)
  • 2021-01-18 06:37

    I've used Sort function in javascript

    function sumTwoSmallestNumbers(numbers){  
      numbers = numbers.sort(function(a, b){return a - b; });
      return numbers[0] + numbers[1];
    };
    

    by providing a compareFunction for the sort functionality array elements are sorted according to the return value of the function.

    0 讨论(0)
  • 2021-01-18 06:37
    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        System.out.println("Enter array size = ");
        int size=in.nextInt();
        int[] n = new int[size];
        System.out.println("Enter "+ size +" values ");
    
        for(int i=0;i<n.length;i++)
            n[i] = in.nextInt();
        int small=n[0],ssmall=n[0];
    
        // finding small and second small
        for(int i=0;i<n.length;i++){
            if(small>n[i]){
                ssmall=small;
                small=n[i];
                }else if(ssmall>n[i])
                    ssmall=n[i];
        }
    
        // finding second small if first element itself small
        if(small==n[0]){
            ssmall=n[1];
            for(int i=1;i<n.length;i++){
                if(ssmall>n[i]){
                    ssmall=n[i];
                    }
            }
        }
    
        System.out.println("Small "+ small+" sSmall "+ ssmall);
    
        in.close();
    }
    
    0 讨论(0)
提交回复
热议问题