java occurs table code example

Example 1: you are given an array of integers. your task is to print the sum of numbers that occurs for an even number of times in the array.

import collections
def fun(arr):
    mp = collections.defaultdict(int)
      
    for i in range(len(arr)):
        mp[arr[i]] += 1 
    sum = 0 
    for i in mp.keys(): 
          
        
        if (mp[i] % 2 == 0): 
            sum += i
    return sum
n= int(input())
arr = list(map(int,input().split()))
print(fun(arr))

Example 2: If there are an odd number of elements in the array, return the element in the middle of the array.

public String middleElement(String[] array)
{
    if (array.length >=1){
        if (array.length %2 != 0){
            return array[array.length/2];
        }
        else if(array.length %2 == 0){
            return array[array.length/2];
        }
        return "";
    }
    else return "";
}

Example 3: Java table

import javax.swing.*;

public class keywords {
    JFrame f;
    keywords(){
        f=new JFrame();
        String data[][]={ {"abstract","assert","boolean","break"},
                {"byte","case","catch","char"},
                {"class","const","continue","default"},
                {"do","double","else","enum"},
                {"extends","final","finally","float"}};
        String column[]={"Keywords","","",""};
        JTable jt=new JTable(data,column);
        jt.setBounds(30,40,200,300);
        JScrollPane sp=new JScrollPane(jt);
        f.add(sp);
        f.setSize(300,400);
        f.setVisible(true);
    }
    public static void main(String[] args) {
        new keywords();
    }

Tags:

Misc Example