简体   繁体   中英

How to find repeating sequence of Integers in an array of Integers in java?

For example if input is:

2 0 6 3 1 6 3 1 6 3 1

then output should be 6 3 1.Need to find the first repetition cycle.

class FindDuplicate
{
void printRepeating(int arr[], int size)
{
    int i; 
    System.out.println("The repeating elements are : ");

    for (i = 0; i < size; i++)
    {
        if (arr[Math.abs(arr[i])] >= 0)
            arr[Math.abs(arr[i])] = -arr[Math.abs(arr[i])];
        else
            System.out.print(Math.abs(arr[i]) + " ");
    }        
} 


public static void main(String[] args) 
{
    FindDuplicate duplicate = new FindDuplicate();
    int arr[] = {1, 2, 3, 1, 2, 3, 1, 2, 3 };
    int arr_size = arr.length;
    duplicate.printRepeating(arr, arr_size);
}
}

https://pastebin.com/12bnjzfw Have used java 8 streams for collection creation.

for (int seqSize = ints.size() / 2; seqSize > 0; seqSize--) { //It should be first cycle. Main priority is biggest sequence
  for (int i = 0; i < ints.size() / seqSize; i++) { //Start position of the first block
    for (int j = i + seqSize; j < ints.size() - seqSize + 1; j++) {
      if (ints.subList(i, i + seqSize).equals(ints.subList(j, j + seqSize))) {
        System.out.println("Answer is: " + ints.subList(i, i + seqSize));
        return;
      }
    }
  }
}

Loop through those array elements and cache your elements until you have the same value for example

List list = Arrays.asList(1,2,3);
if(list.contains(element))
 //code to check here

you will need get size of your list and based on that check exactly this amount of items if they match if yes then output if not clear cache and start cache from the begging.

class FindDuplicate {
    static int STRING_LENGTH = 3;

    void printRepeating(int arr[], int size) {
        int i; 
        System.out.println("The repeating elements are : ");
        String strVal = "";
        for (int ii = 0; ii < size; ii++) {
            strVal += arr[ii]; 
        }
        // strVal now has something we can search with.

        for (i = 0; i < size; i++) {
            int end = Math.min(size,i+STRING_LENGTH );
            String searchString = strVal.substring(i, end);
            if (searchString.length() != STRING_LENGTH)
                break;  // at end of arr, doesn't have length to search 
            int matchIndex = strVal.indexOf(searchString, i+1);
            if (matchIndex != -1) {
               String match = strVal.substring(matchIndex, matchIndex + STRING_LENGTH);
               System.out.print(match + " ");
               break; // done with loop
            }
        }        
    } 


    public static void main(String[] args) {
        FindDuplicate duplicate = new FindDuplicate();
        int arr[] = {1, 2, 3, 1, 2, 3, 1, 2, 3 };
        int arr_size = arr.length;
        duplicate.printRepeating(arr, arr_size);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM