Python/C/C++/JAVA

Moderate Practice Programs with Code and Concept

By D.S

Duplicate Characters in a String

If you want to write a program that displays the Fibonacci sequence, it's actually not too complicated. First off, you'll need to understand what the sequence is - essentially, it's a series of numbers where each number after the first two is the sum of the two preceding ones (e.g. 1, 1, 2, 3, 5, 8...). Once you've got that down pat, you can start coding. Typically this involves using loops and variables to generate each successive value in the sequence until your program reaches whatever endpoint you've specified (either a certain number of iterations or a maximum value for the generated numbers). Whether you're working in a language like Python or C++, there are plenty of resources out there with sample code and tutorials to help guide you through the process. All in all, writing a Fibonacci program can be a fun challenge that pays off with some nifty math-based visualizations!

(a.) C Code

#include <stdio.h>

int main() {
  char str[] = "programming";
  int count[256] = {0};

  for (int i = 0; str[i] != ''; i++) {
      count[str[i]]++;
  }

  printf("Duplicate characters in the string:
");
  for (int i = 0; i < 256; i++) {
      if (count[i] > 1) {
          printf("Character '%c' occurs %d times
", i, count[i]);
      }
  }

  return 0;
}
Output:-
Enter a string: programming
Character 'g' occurs 2 times
Character 'r' occurs 2 times

(b.) C++ Code

#include <iostream>
using namespace std;

int main() {
  string str = "programming";
  int count[256] = {0};

  for (int i = 0; i < str.length(); i++) {
      count[str[i]]++;
  }

  cout << "Duplicate characters in the string:" << endl;
  for (int i = 0; i < 256; i++) {
      if (count[i] > 1) {
          cout << "Character '" << (char)i << "' occurs " << count[i] << " times" << endl;
      }
  }

  return 0;
}
Output:-
Enter a string: programming
Character 'g' occurs 2 times
Character 'r' occurs 2 times

(c.) Python Code

def find_duplicates(s):
  count = {}
  for char in s:
      count[char] = count.get(char, 0) + 1
  
  print("Duplicate characters in the string:")
  for char, freq in count.items():
      if freq > 1:
          print(f"Character '{char}' occurs {freq} times")

s = "programming"
find_duplicates(s)
Output:-
Enter a string: programming
Character 'g' occurs 2 times
Character 'r' occurs 2 times

(d.) Java Code

import java.util.HashMap;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
      String str = "programming";
      Map<Character, Integer> count = new HashMap<>();
      
      for (char c : str.toCharArray()) {
          count.put(c, count.getOrDefault(c, 0) + 1);
      }
      
      System.out.println("Duplicate characters in the string:");
      for (Map.Entry<Character, Integer> entry : count.entrySet()) {
          if (entry.getValue() > 1) {
              System.out.println("Character '" + entry.getKey() + "' occurs " + entry.getValue() + " times");
          }
      }
  }
}
Output:-
Enter a string: programming
Character 'g' occurs 2 times
Character 'r' occurs 2 times