Home > database >  Get rid of duplicate character that are not necessarily adjacent
Get rid of duplicate character that are not necessarily adjacent

Time:09-15

So I was asked to have two different arrays that both stored 5 random numbers and if any number between each array were the same, they would be printed out.

// Creates Arrays   
for (int i = 0; i < 5; i  )
        {
            first5num[i] = rand.nextInt(9   1);
            System.out.print(" "   first5num[i]);
        }

        for (int j = 0; j < 5; j  )
        {
            second5num[j] = rand.nextInt(9   1);
            System.out.print(" "   second5num[j]);
        }

//Loops through arrays to find duplicates
        for (int k = 0; k < 5; k  )
        {
            comparitive = first5num[k];
            for (int l = 0; l < 5; l  )
            {
                if (comparitive == second5num[l])
                {
                    SameNums  = comparitive;
                }
            }
        }
    

I was able to get it working and it would print out the shared numbers between both arrays. I stored the Shared numbers into a string called SameNums and now I must ensure that the string does not hold any duplicate numbers. For example Array1 = 1, 2, 3, 4, 5 and Array2 = 1, 2, 3, 5, 5. The SameNums string would hold "12355". How do I ensure that 5 is only listed once and please keep in mind that the string may include duplicates that are not directly next to each other (12928 should get rid of the second 2. Below is my train of thought so far:

//Manipulates string using while loop
            while (run == true)
            {
            if (counter == SameNums.length() - 1)
            {
                run = false;
            }
            if (SameNums.charAt(counter) == SameNums.charAt(restrict))
            {
                word1 = SameNums.substring(0, restrict);
                word3 = SameNums.substring(restrict   1, SameNums.length());
                SameNums = word1   word3;
                counter  ;
                restrict = counter   1;
                continue;
            }
    

How can I make the string read no duplicate chars?*

CodePudding user response:

//add random integers into a common array list, than filter that list "duplicates" for duplicates.

    Random rand = new Random();
    ArrayList<Integer> duplicates = new ArrayList<>();
    ArrayList<Integer> first5num = new ArrayList<>();
    ArrayList<Integer> second5num = new ArrayList<>();
    for (int i = 0; i < 5; i  ) {
        first5num.add(i, rand.nextInt(10));
        duplicates.add(i, first5num.get(i));

    }

    for (int j = 0; j < 5; j  ) {
        second5num.add(j, rand.nextInt(10));
        duplicates.add(j, second5num.get(j));

    }

    Map<Integer, Long> frequencies = duplicates.stream()
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    frequencies.entrySet().stream()
            .filter(entry -> entry.getValue() > 1)
            .forEach(entry -> System.out.println(entry.getKey()));

CodePudding user response:

//if you manually enter random int values for both arrays
        Random rand = new Random();
        ArrayList<Integer> duplicates = new ArrayList<>();
        ArrayList<Integer> first5num = new ArrayList<>();
        ArrayList<Integer> second5num = new ArrayList<>();
        first5num.add(4);
        first5num.add(7);
        first5num.add(9);
        first5num.add(10);
        first5num.add(4);

        second5num.add(4);
        second5num.add(6);
        second5num.add(5);
        second5num.add(0);
        second5num.add(4);
        for (int i = 0; i < 5; i  ) {
            duplicates.add(i, first5num.get(i));
        }
        for (int j = 0; j < 5; j  ) {
            duplicates.add(j, second5num.get(j));
        }

        Map<Integer, Long> frequencies = duplicates.stream()
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        frequencies.entrySet().stream()
                .filter(entry -> entry.getValue() > 1)
                .forEach(entry -> System.out.println(entry.getKey()));
  • Related