Java Array Exercises Practice Solution I

1. Write a Java program to reverse an array of integer values.

package com.w3resource;

import java.util.Arrays;

public class ReverseArray {
    
    public static void main(String[] args) {
        
        int[] my_array = {5, 2, 7, 9, 6};
        System.out.println("Original array: " + Arrays.toString(my_array));
        
        for (int i = 0; i < my_array.length / 2; i++) {

            int temp = my_array[i];
            my_array[i] = my_array[my_array.length - i - 1];
            my_array[my_array.length - i - 1] = temp;
        }
        System.out.println("Reverse array: " + Arrays.toString(my_array));
    }
}

2. Write a Java program to find the duplicate values of an array of integer values.

package com.w3resource;

public class FindDuplicateElement {
    
    public static void main(String[] args) {
        
        int[] my_array = {5, 2, 7, 7, 5};
        
        for (int i = 0; i < my_array.length - 1; i++) {
            for (int j = i + 1; j < my_array.length; j++) {
                
                if ((my_array[i] == my_array[j]) && (i != j)) {
                    
                    System.out.println("Duplicate Element: " + my_array[j]);
                }
            }
        }
    }
}

3. Write a Java program to find the duplicate values of an array of string values.

package com.w3resource;

public class FindDuplicateString {
    
    public static void main(String[] args) {
        
        String[] my_array = {"bcd", "abd", "jude", "bcd", "oiu"};
        
        for (int i = 0; i < my_array.length - 1; i++) {
            
            for (int j = i + 1; j < my_array.length; j++) {
                
                if ( (my_array[i].equals(my_array[j])) && (i != j) ) {
                    
                    System.out.println("Duplicate Element is: " + my_array[j]);
                }
            }
        }
    }
}

4. Write a Java program to find the common elements between two arrays (string values).

package com.w3resource;

import java.util.Arrays;
import java.util.HashSet;

public class FindCommonElement {
    
    public static void main(String[] args) {
        
        String[] my_array = {"Python", "JAVA", "PHP", "C#", "C++", "SQL"};
        String[] new_array = {"MySQL", "SQL", "SQLite", "Oracle", "PostgreSQL", "DB2", "JAVA"};
        
        System.out.println("my_array: " + Arrays.toString(my_array));
        System.out.println("new_array: " + Arrays.toString(new_array));
        
        HashSet<String> set = new HashSet<String>();
        
        for (int i = 0; i < my_array.length; i++) {
            
            for (int j = 0; j < new_array.length; j++) {
                
                if (my_array[i].equals(new_array[j])) {
                    set.add(my_array[i]);
                }
            }
        }
        
        System.out.println("Common Element is: " + set);
    }
}

Output

Array1 : [Python, JAVA, PHP, C#, C++, SQL]                                                                    
Array2 : [MySQL, SQL, SQLite, Oracle, PostgreSQL, DB2, JAVA]                                                  
Common Element is : [JAVA, SQL]

5. Write a Java program to find the common elements between two arrays of integers.

package com.w3resource;

import java.util.Arrays;

public class CommonIntegerElement {
    
    public static void main(String[] args) {
        
        int[] my_array = {1, 2, 5};
        int[] new_array = {6, 5, 8};
        
        System.out.println("my_array: " + Arrays.toString(my_array));
        System.out.println("new_array: " + Arrays.toString(new_array));
        
        for (int i = 0; i < my_array.length; i++) {
            
            for (int j = 0; j < new_array.length; j++) {

                if (my_array[i] == new_array[j]) {
                    
                    System.out.println("Common Element is : " + array[i])
                }            
 	       }
    	}
    }
}

6. Write a Java program to remove duplicate elements from an array.

package com.zetcode;

import java.util.Arrays;

public class RemoveDuplicateElements {

    static void unique_array(int[] my_array) {

        System.out.println("Original Array : ");

        for (int i = 0; i < my_array.length; i++) {
            System.out.print(my_array[i] + "\t");
        }

        //Assuming all elements in input array are unique

        int no_unique_elements = my_array.length;

        //Comparing each element with all other elements

        for (int i = 0; i < no_unique_elements; i++) {
            for (int j = i + 1; j < no_unique_elements; j++) {
                //If any two elements are found equal

                if (my_array[i] == my_array[j]) {
                    //Replace duplicate element with last unique element

                    my_array[j] = my_array[no_unique_elements - 1];

                    no_unique_elements--;

                    j--;
                }
            }
        }

        // Copying only unique elements of my_array into array1

        int[] new_array = Arrays.copyOf(my_array, no_unique_elements);

        // Printing array Without Duplicates

        System.out.println();

        System.out.println("Array with unique values : ");

        for (int i = 0; i < new_array.length; i++) {
            System.out.print(new_array[i] + "\t");
        }

        System.out.println();

        System.out.println("---------------------------");
    }

    public static void main(String[] args) {
        unique_array(new int[]{0, 3, -2, 4, 3, 2});

        unique_array(new int[]{10, 22, 10, 20, 11, 22});

    }
}

Output

Original Array : 
0	3	-2	4	3	2	
Array with unique values : 
0	3	-2	4	2	
---------------------------
Original Array : 
10	22	10	20	11	22	
Array with unique values : 
10	22	11	20	
---------------------------

7. Write a Java program to find the second largest element in an array.

package com.zetcode;

import java.util.Arrays;

public class FindSecondLargest {

    public static void main(String[] args) {

        int[] my_array = {10789, 2035, 1899, 2046, 2049, 1456, 2013};

        System.out.println("Original numeric array: " + Arrays.toString(my_array));
        Arrays.sort(my_array);
        int index = my_array.length - 1;
        while (my_array[index] == my_array[my_array.length - 1]) {
            index--;
        }
        System.out.println("Second largest value: " + my_array[index]);
    }
}

8. Write a Java program to find the second smallest element in an array.

package com.zetcode;

import java.util.Arrays;

public class FindSecondSmallest {

    public static void main(String[] args) {

        int[] my_array = {-1, 4, 0, 2, 7, -3};
        System.out.println("Original numeric array : " + Arrays.toString(my_array));
        int min = Integer.MAX_VALUE;
        int second_min = Integer.MAX_VALUE;
        for (int i = 0; i < my_array.length; i++) {
            if (my_array[i] == min) {
                second_min = min;
            } else if (my_array[i] < min) {
                second_min = min;
                min = my_array[i];
            } else if (my_array[i] < second_min) {
                second_min = my_array[i];
            }

        }
        System.out.println("Second lowest number is : " + second_min);
    }
}
posted @ 2020-06-06 23:53  PrimerPlus  阅读(257)  评论(0编辑  收藏  举报