A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Java program to find the common strings in two string arrays
Q:

Java program to find the common strings in two string arrays

0

Example:

    Example:
    Input:
    Array 1 elements: C, C++, C#, JAVA, SQL, ORACLE
    Array 2 elements: MySQL, SQL, Android, ORACLE, PostgreSQL, DB2, JAVA 
    Output:
    Common elements: JAVA, ORACLE, SQL

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to find common strings in two string arrays in java

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

public class ExArrayCommon {
  public static void main(String[] args) {
    // enter string value.
    String[] array1 = {
      "C",
      "C++",
      "C#",
      "JAVA",
      "SQL",
      "ORACLE"
    };

    String[] array2 = {
      "MySQL",
      "SQL",
      "Android",
      "ORACLE",
      "PostgreSQL",
      "DB2",
      "JAVA"
    };

    // print both the string.
    System.out.println("Array1 : " + Arrays.toString(array1));
    System.out.println("Array2 : " + Arrays.toString(array2));

    HashSet < String > set = new HashSet < String > ();

    for (int i = 0; i < array1.length; i++) {
      for (int j = 0; j < array2.length; j++) {
        if (array1[i].equals(array2[j])) {
          set.add(array1[i]);
        }
      }
    }
    // return common elements.
    System.out.println("Common element : " + (set));
  }
}

Output

    Array1 : [C, C++, C#, JAVA, SQL, ORACLE]
    Array2 : [MySQL, SQL, Android, ORACLE, PostgreSQL, DB2, JAVA]
    Common element : [JAVA, ORACLE, SQL]

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now