/home/caleb/ASDV-Java/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/ShuffleArrayList.java
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.calebfontenot.mp5_calebfontenot;

import java.util.*;

/**
 *
 * @author caleb
 */
public class ShuffleArrayList {

public static void shuffle(ArrayList<Number> list) {
    // Create a new Random object.
    Random rng = new Random();
    // Create an ArrayList to store the indices of the elements that have been selected.
    ArrayList<Integer> selectedIndices = new ArrayList<Integer>();

    // Loop through each element in the list.
    for (int i = 0; i < list.size(); ++i) {
        // Generate a random index that has not been selected before.
        int randomIndex;
        do {
            randomIndex = rng.nextInt(list.size()); // Generate a random integer between 0 (inclusive) and the size of the list (exclusive).
        } while (selectedIndices.contains(randomIndex)); // Repeat until an unselected index is found.
        selectedIndices.add(randomIndex); // Add the selected index to the list of selected indices.
        //System.out.println(randomIndex + ", " + i);
        // Swap the element at the random index with the element at the current index of the loop.
        // This shuffles the list by randomly selecting an element to swap with the current element at each iteration.
        Number temp = list.get(randomIndex); // Save the element at the random index to a temporary variable.
        list.set(randomIndex, list.get(i)); // Overwrite the element at the random index with the element at the current index of the loop.
        list.set(i, temp); // Set the current index of the loop to the saved element, effectively swapping the two elements.
    }
}


    public static String checkForDuplicates(ArrayList<Number> list) {
        // Ensure Array does not include repeat numbers.
        boolean repeatNumber = false;
        for (int i = 0; i < list.size(); ++i) {
            for (int j = 0; j < list.size(); ++j) {
                if (i != j) {
                    //System.out.println("Checking " + list.get(i) + " and " + list.get(j));
                    if (list.get(i) == list.get(j)) {
                        repeatNumber = true;
                    }
                }
            }
        }
        if (repeatNumber) {
            return "Numbers repeat in ArrayList.";
        } else {
            return "Numbers do not repeat in ArrayList.";
        }
    }

    public static void main(String[] args) {
        final int ARRAY_SIZE = 50;
        ArrayList<Number> list = new ArrayList<>();
        for (int i = 0; i < ARRAY_SIZE; ++i) {
            list.add(i + 1); // Fill ArrayList with sequential numbers.
        }

        System.out.println(list);
        System.out.println(checkForDuplicates(list));
        shuffle(list);
        System.out.println(list);
        System.out.println(checkForDuplicates(list));
    }

}