Q: Sequences of Equal Strings - Java Task

+4 votes

Write a program that enters an array of strings and finds in it all sequences of equal elements. The input strings are given as a single line, separated by a space. Note: the count of the input strings is not explicitly specified, so you might need to read the first input line as a string and split it by a space.


sequences of equal strings injava

asked in Java category by user hues
edited by user golearnweb

1 Answer

+1 vote
Best answer

Here's my solution:

import java.util.ArrayList;
import java.util.Scanner;

public class Pr_02_SequencesOfEqualStrings {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(;

        String[] array = scanner.nextLine().split(" ");
        ArrayList<String> list = new ArrayList<>();

        for (int i = 0; i < array.length; i++) {
            if (!list.contains(array[i])) {
                for (int j = 0; j < array.length; j++) {
                    if (array[i].equals(array[j])) {
                        System.out.print(array[i] + " ");


answered by user john7
selected by user golearnweb