1

I'd like to print only words with repeating characters from an array. For example, the following code prints the whole array, but what if I only wanted it to print "hello" because it has a repeating character or any other words with repeating characters and not output words without repeating characters.

public class iterateThruArray
{
    public static void main(String[] args) 
    {
        String[] words = {"hello", "lmao", "why"};
        int len = words.length;

        for(int i = 0; i < len; i++)
        {
            String word = words[i];
            //prints array words
            System.out.println(word);
        }

    }
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • You need an inner for-loop to see if any consecutive characters are the same. – SedJ601 Nov 20 '19 at 17:20
  • 2
    What have you tried? What would be your strategy? – JB Nizet Nov 20 '19 at 17:20
  • @JBNizet Well the obvious answer would be iterate through a word and check for duplicate character then place it in another array. But I dont know how I would implement this tbh. – ModernDayTechie Nov 20 '19 at 17:24
  • 1
    You don't need to place it in another array. All you need to do is to print it if it has repeating characters, and to not print it if it doesn't. So thebody of your loop should be `String word = words[i]; if (hasRepeatingCharacters(word) { System.out.println(word); }`. Now you can concentrate on the method `boolean hasRepeatingCharacters(String word)`. Suppose it's a really really long word and you can't read it all at once in your head. You have a pen and a paper to help you. How would you do it? – JB Nizet Nov 20 '19 at 17:27

5 Answers5

0

Now, you can call hasRepeatingChars(words[i]); from you for loop, and only print if it returns true.

    public boolean hasRepeatingChars(String word) {
        char previous = '%'; // some char you know won't be in the string

        for (int i = 0; i < word.length(); i++) {
            char current = word.charAt(i);
            if (current == previous) {
                return true;
            }
        }

        return false;
    }

Edit: I see now that someone has commented working this out on your own above... don't peek!

sleepToken
  • 1,866
  • 1
  • 14
  • 23
0

Try the following

 public class iterateThruArray
    {
        public static boolean distinct(String word)
        {
            char[] ch = word.toCharArray();
            Arrays.sort(ch);
            for (int i = 0; i < ch.length - 1; i++) 
            { 
                //After sorting the array, all repeating characters(if any) would have to appear adjacent to each other, hence we just check for repeating adjacent characters
                if (ch[i] == ch[i + 1]) 
                    return false; 
            } 
            return true;   
        }

        public static void main(String[] args) 
        {
            String[] words = {"hello", "lmao", "why"};
            int len = words.length;

            for(int i = 0; i < len; i++)
            {
                String word = words[i];
                if(distinct(word))
                    System.out.println(word);
            }

        }
    }
Varun Mukundhan
  • 260
  • 2
  • 10
0

You can have a 32 bit checker and check each letter of the string. If the “AND” operation with the checker is greater than 0, then there is a duplicAte.

Code looks like this:

  boolean IsUniqueString (string input)
 {

    int checkValue = 0; 

    for (int temp = 0; temp < str.length(); temp++) { 

        int bitIndex = input.charAt(temp) - 'a’;

        if ((checkValue & (1 << bitIndex)) > 0) 

            return false; 

        checkValue= checkValue | (1 << bitIndex); 

    return true; 
  } 
Gauravsa
  • 6,330
  • 2
  • 21
  • 30
0

What do you think about this solution?

    public static void main(String[] args) 
    {
        // some words ..
        String[] words = {"hello", "lmao", "why", "1abc1", "letter", "middle"};

        int len = words.length;

        for(int i = 0; i < len; i++) {
            String word = words[i];
            if(hasRepeatingChars(word)) {
                System.out.println(word);
            }
        }

    }

    public static boolean hasRepeatingChars(String word) {
        // here we will hold the characters and count for double letters
        LinkedList<Character> ll = new LinkedList<>();

        for (int i = 0; i < word.length(); i++) {           
            // we iterate now through the letters and get the current letter
            Character current = word.charAt(i);
            // we add here the current letter to the list
            ll.add(current);

            // when the position is at the first letter do not go into this block
            if(i > 0) {
                // ok from the second letter on we check the neighbours :)
                Character neighbour1 = ll.get(ll.size() -1);
                Character neighbour2 = ll.get(ll.size() -2);

                // .. if they are equal return true
                if(neighbour1.equals(neighbour2)) {
                    return true;
                }

            }

        }

        return false;
    }
greg
  • 3
  • 4
0

If set.size() != word.length() then the word has repeating characters.

private static void printRepeatingWords(String[] words) {
    Arrays.stream(words).forEach(word->{
        Set<Character> set = new HashSet<>();
        for (char c : word.toCharArray()) set.add(c);
        if (set.size() != word.length()) System.out.println(word);
    });
}
Ankit Sharma
  • 1,626
  • 1
  • 14
  • 21