[Solved] Sequences of Equal Strings - Java Task

  

3
Topic starter

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.

Examples:

sequences of equal strings injava

1 Answer
2

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(System.in);
 
        String[] array = scanner.nextLine().split(" ");
        ArrayList<String> list = new ArrayList<>();
 
        for (int i = 0; i < array.length; i++) {
            if (!list.contains(array[i])) {
                list.add(array[i]);
                for (int j = 0; j < array.length; j++) {
                    if (array[i].equals(array[j])) {
                        System.out.print(array[i] + " ");
                    }
                }
                System.out.println();
            }
        }
    }
}
Share: