Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

public class Main {

    public static List<String> getAllSameElement2(String[] strArr1, String[] strArr2) {
        if (strArr1 == null || strArr2 == null) {
            return null;
        }
        Arrays.sort(strArr1);
        Arrays.sort(strArr2);
        List<String> list = new ArrayList<String>();
        int k = 0;
        int j = 0;
        while (k < strArr1.length && j < strArr2.length) {
            if (strArr1[k].compareTo(strArr2[j]) == 0) {
                if (strArr1[k].equals(strArr2[j])) {
                    list.add(strArr1[k]);
                    k++;
                    j++;
                }
                continue;
            } else if (strArr1[k].compareTo(strArr2[j]) < 0) {
                k++;
            } else {
                j++;
            }
        }
        return list;
    }
}