2

I am trying to make a code with two arrays. The second array has the same values of the first except for the smallest number. I have already made a code where z is the smallest number. Now I just want to make a new array without z, any feedback would be appreciated.

public static int Second_Tiny() {
    int[] ar = {19, 1, 17, 17, -2};
    int i;
    int z = ar[0];

    for (i = 1; i < ar.length; i++) {
        if (z >ar[i]) {  
            z=ar[i];  
        }                   
    }
}
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • I am trying to remove the smallest element within the code itself though. Is there anything I can do to remove a variable z? – Ruchir Baronia Jul 28 '15 at 00:55
  • do you want to do what this code is doing in different way i.e. without using `z` or something else ? Why is method called `Second_Tiny`? – Shahzeb Jul 28 '15 at 00:56
  • I am trying to find out the second smallest integer in array ar[]. I should get an output of 1 once I am done. The way I want to achieve that is by making a new array called newar[] and make it include all the indexes of ar[], except without -2. – Ruchir Baronia Jul 28 '15 at 00:58
  • Simply create a new array, like `int[] newArray = new int [ ar.length - 1 ];`. Now simply copy all values to this array from `ar` except the smallest value. – nIcE cOw Jul 28 '15 at 00:59
  • ArrayUtils.remove(ar[1], z); – Ruchir Baronia Jul 28 '15 at 00:59
  • okay starting to make sense . however your method is returning an int not an array even if you create new array that would be pointless. – Shahzeb Jul 28 '15 at 00:59
  • I get an error saying: ArrayUtils cannot be resolved – Ruchir Baronia Jul 28 '15 at 01:00
  • `ArrayUtils` is a third part API ( [Apache Commons](https://commons.apache.org/proper/commons-lang/download_lang.cgi) ) – nIcE cOw Jul 28 '15 at 01:00

3 Answers3

4

Java 8 streams have built in functionality that can achieve what you're wanting.

public static void main(String[] args) throws Exception {
    int[] ar = {19, 1, 17, 17, -2, -2, -2, -2, 5};

    // Find the smallest number
    int min = Arrays.stream(ar)
            .min()
            .getAsInt();

    // Make a new array without the smallest number
    int[] newAr = Arrays
            .stream(ar)
            .filter(a -> a > min)
            .toArray();

    // Display the new array
    System.out.println(Arrays.toString(newAr));
}

Results:

[19, 1, 17, 17, 5]

Otherwise, you'd be looking at something like:

public static void main(String[] args) throws Exception {
    int[] ar = {19, 1, 17, 17, -2, -2, -2, -2, 5};

    // Find the smallest number
    // Count how many times the min number appears
    int min = ar[0];
    int minCount = 0;
    for (int a : ar) {
        if (minCount == 0 || a < min) {
            min = a;
            minCount = 1;
        } else if (a == min) {
            minCount++;
        }
    }

    // Make a new array without the smallest number
    int[] newAr = new int[ar.length - minCount];
    int newIndex = 0;
    for (int a : ar) {
        if (a != min) {
            newAr[newIndex] = a;
            newIndex++;
        }
    }

    // Display the new array
    System.out.println(Arrays.toString(newAr));
}

Results:

[19, 1, 17, 17, 5]
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
  • 2
    Note that if the smallest number is repeated several times, in the stream solution you will remove all instances of it, but plain solution will leave zeroes at the end of array (which seems incorrect to me). It's unclear from the question though whether OP wants to handle such situation. – Tagir Valeev Jul 28 '15 at 02:16
  • 1
    @TagirValeev Even if the OP wasn't wanting to handle such a situation, I've updated the plain solution to remove all instances of the minimum number. – Shar1er80 Jul 28 '15 at 02:25
  • 1
    You can merge first and second pass together using like `int min=0,minCount=0; for(int a:ar) if(minCount==0 || a – Tagir Valeev Jul 28 '15 at 04:05
2

I think the OP is on wrong track seeing his this comment:

"I am trying to find out the second smallest integer in array ar[]. I should get an output of 1 once I am done. The way I want to achieve that is by making a new array called newar[] and make it include all the indexes of ar[], except without -2."

This is a very inefficient way to approach this problem. You'll have to do 3 passes, Once to find to smallest indexed element, another pass to remove the element (this is an array so removing an element will require a full pass), and another one to find smallest one again.

You should just do a single pass algorithm and keep track of the smallest two integers, or even better use a tree for efficiency. Here are the best answers of this problem:

Find the 2nd largest element in an array with minimum number of comparisons

Algorithm: Find index of 2nd smallest element from an unknown array

UPDATE: Here is the algorithm with OP's requirements, 3 passes, and no external libraries:

public static int Second_Tiny() {

  int[] ar = {19, 1, 17, 17, -2};

  //1st pass - find the smallest item on original array  
  int i;
  int z = ar[0];
  for (i = 1; i < ar.length; i++) {
    if (z >ar[i]){
      z=ar[i];
    }
  }

  //2nd pass copy all items except smallest one to 2nd array
  int[] ar2 = new int[ar.length-1];
  int curIndex = 0;
  for (i=0; i<ar.length; i++) {
    if (ar[i]==z)
      continue;
    ar2[curIndex++] = ar[i];
  }

  //3rd pass - find the smallest item again
  z = ar2[0];
  for (i = 1; i < ar2.length; i++) {
    if (z >ar2[i]){
      z=ar2[i];
    }
  }

  return z;
}
Community
  • 1
  • 1
ttekin
  • 827
  • 8
  • 9
0

This grabs the index of the element specified in variable z and then sets a second array to the first array minus that one element.

Essentially this gives ar2 = ar1 minus element z

public static int Second_Tiny() {
    int[] ar = {19, 1, 17, 17, -2};
    int[] ar2;
    int i;
    int z = ar[0];
    int x = 0;

    for (i = 1; i < ar.length; i++) {
        if (z >ar[i]){  
            z=ar[i];
            x=i;  
        }                   
    }
    ar2 = ArrayUtils.remove(ar, x);
    return(z);    
}
TomDillinger
  • 194
  • 7
  • With this, if negative 2 wasn't element 4, the code wouldn't work. Is there anyway I can do it without having to worry about order? – Ruchir Baronia Jul 28 '15 at 01:08
  • This should work fine regardless of order, unless the value you put for "Z" isn't what you want to pull out. the "X" variable is just getting the index of whatever variable "Z" is set to. I recommend outputting "Z" and the elements or "ar2" so you can see what they actually contain. :) – TomDillinger Jul 28 '15 at 01:18